>  기사  >  백엔드 개발  >  CodeIgniter 프레임워크 검증 코드 라이브러리 파일 및 사용법 분석

CodeIgniter 프레임워크 검증 코드 라이브러리 파일 및 사용법 분석

不言
不言원래의
2018-06-14 14:02:251770검색

이 글은 주로 CodeIgniter 프레임워크 검증 코드 클래스 라이브러리 파일과 사용법을 소개하고, CodeIgniter 프레임워크 검증 코드 클래스 라이브러리 파일의 정의와 구체적인 사용법을 예제 형식으로 분석합니다. 필요한 친구들이 참고할 수 있습니다. it

이 글의 예제에서는 CodeIgniter 프레임워크 검증 코드 클래스 라이브러리 파일과 사용법을 설명합니다. 참고하실 수 있도록 자세한 내용은 다음과 같습니다.

4~5시간을 헤매다 드디어 CI 인증코드 라이브러리가 성공적으로 완성되었습니다.

아래 소스 코드를 참조하세요.

애플리케이션/라이브러리에서 Authcode.php 파일을 생성하세요. 코드는 다음과 같습니다:

<?php
class Authcode
{
 var $CI;
 var $fontPath;//字体路径
 var $image;
 var $charLen   = 4; //生成几位验证码
 var $arrChr   = array();//验证码字符
 var $width    = 83; //图片宽
 var $height   = 24; //图片高
 var $bgcolor   = "#ffffff"; //背景色
 var $showNoisePix  = true; //生成杂点
 var $noiseNumPix  = 80; //生成杂点数量
 var $showNoiseLine  = true; //生成杂线
 var $noiseNumLine  = 2; //生成杂线数量
 var $showBorder  = true; //边框,当杂点、线一起作用的时候,边框容易受干扰
 var $borderColor  = "#000000";
 function Authcode()
 {
  $this->CI = & get_instance();
  $this->fontPath = realpath(dirname(__FILE__) . &#39;/fonts/&#39;); //字体文件
  //$this->arrChr   = array_merge(range(1, 9) , range(&#39;A&#39;, &#39;Z&#39;));//数字字母验证码
  //$this->arrChr   = range(&#39;A&#39;, &#39;Z&#39;);//纯字母验证码
  $this->arrChr = range(0, 9);//纯数字验证码
 }
 /**
  * 显示验证码
  *
  */
 function show()
 {
  $this->image = imageCreate($this->width, $this->height);
  $this->back = $this->getColor($this->bgcolor);
  imageFilledRectangle($this->image, 0, 0, $this->width, $this->height, $this->back);
  $size = $this->width / $this->charLen - 4;
  if ($size > $this->height) {
   $size = $this->height;
  }
  $left = ($this->width - $this->charLen * ($size + $size / 10)) / $size + 5;
  $code = &#39;&#39;;
  for($i = 0; $i < $this->charLen; $i ++) {
   $randKey = rand(0, count($this->arrChr) - 1);
   $randText = $this->arrChr[$randKey];
   $code .= $randText;
   $textColor = imageColorAllocate($this->image, rand(0, 100), rand(0, 100), rand(0, 100));
   $font = $this->fontPath . &#39;/&#39; . rand(1, 5) . ".ttf";
   $randsize = rand($size - $size / 10, $size + $size / 10);
   $location = $left + ($i * $size + $size / 10);
   @imagettftext($this->image, $randsize, rand(- 18, 18), $location, rand($size - $size / 10, $size + $size / 10) + 2, $textColor, $font, $randText);
  }
  if ($this->showNoisePix == true) {
   $this->setNoisePix();
  }
  if ($this->showNoiseLine == true) {
   $this->setNoiseLine();
  }
  if ($this->showBorder == true) {
   $this->borderColor = $this->getColor($this->borderColor);
   imageRectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $this->borderColor);
  }
  $this->CI->session->set_userdata(&#39;auth_code&#39;, $code);
  ob_clean();
  header("Content-type: image/jpeg");
  imagejpeg($this->image);
  imagedestroy($this->image);
 }
 /**
  * 显示验证码的JS调用
  *
  */
 function showScript()
 {
  //显示验证码
  echo "var img_src = &#39;/imgauthcode/show/?&#39;;\n";
  echo "document.writeln(&#39;<img id=\"img_authcode\" src=\"&#39; + img_src + Math.random() + &#39;\" style=\"cursor:hand;\" onclick=\"this.src=img_src + Math.random();\" alt=\"点击更换图片\">&#39;);";
 }
 /**
  * 检查验证码是否正确
  *
  * @param string $auth_code
  * @return bool
  */
 function check($auth_code = null)
 {
  return ($this->CI->session->userdata(&#39;auth_code&#39;) && $auth_code) ? ($this->CI->session->userdata(&#39;auth_code&#39;) === $auth_code) : false;
 }
 function getColor($color)
 {
  $color = eregi_replace("^#", "", $color);
  $r = $color[0] . $color[1];
  $r = hexdec($r);
  $b = $color[2] . $color[3];
  $b = hexdec($b);
  $g = $color[4] . $color[5];
  $g = hexdec($g);
  $color = imagecolorallocate($this->image, $r, $b, $g);
  return $color;
 }
 function setNoisePix()
 {
  for($i = 0; $i < $this->noiseNumPix; $i ++) {
   $randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
   imageSetPixel($this->image, rand(0, $this->width), rand(0, $this->height), $randColor);
  }
 }
 function setNoiseLine()
 {
  for($i = 0; $i < $this->noiseNumLine; $i ++) {
   $randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
   imageline($this->image, rand(1, $this->width), rand(1, $this->height), rand(1, $this->width), rand(1, $this->height), $randColor);
  }
 }
}

Authcode.php 코드가 끝납니다.

컨트롤러에 관리자가 있습니다. 두 가지 메소드가 있는 클래스입니다.

Class Admin extends CI_Controller{
 function __construct()
 {
  parent::__construct();
  $this->load->library(&#39;Authcode&#39;);
 }
function captcha(){
  if($_POST){
    if ($this->authcode->check($this->input->post(&#39;gd_pic&#39;))) {
    echo "right";
   } else {
    echo &#39;验证码不正确,请重新输入&#39;;
   }
  }else{
   $this->load->view(&#39;demo&#39;);
  }
 }
 function show_captcha(){ //此方法用于显示验证码图片,归一个view中的img的src调用
  $this->authcode->show();
 }
}

다음은 뷰에 데모.php를 생성하는 것입니다. 코드는 다음과 같습니다.

<?php echo form_open(&#39;c=admin&m=captcha&#39;);?>
<input type="text" name="gd_pic" />
<img src="<?php echo base_url(&#39;?c=admin&m=show_captcha&#39;);?>" ><br>
<input type="submit" name="submit" value="验证" />
<?php echo form_close();?>

OK입니다. 모든 것이 끝났고 마침내 정상적으로 실행됩니다. .

위 내용은 이 글의 전체 내용입니다. 모든 분들의 학습에 도움이 되었으면 좋겠습니다. 더 많은 관련 내용은 PHP 중국어 홈페이지를 주목해주세요!

관련 권장 사항:

CI 프레임워크의 양식 유효성 검사 분석 정보

Nginx 및 CI 프레임워크에서 404 오류를 해결하는 방법

위 내용은 CodeIgniter 프레임워크 검증 코드 라이브러리 파일 및 사용법 분석의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.