PHP 인증 코드 검사 기능 구현

인증번호 의 검 사 는 PHP 의 SESSION 기능 을 이용 하여 이 루어 집 니 다.
맨 위 에 함수 sessionstart(); 서버 에 우리 가 이 함수 의 기능 을 사용 해 야 한다 고 알려 주세요.

session_start(); 
다음 에 우리 가 사용 하 는 것 은 바로 인증 코드 가 실 현 된 코드 이다.여기에 영문 숫자의 코드 를 예 로 들 자.

$image = imagecreatetruecolor(100, 30); //    100×30    
$white = imagecolorallocate($image,255,255,255);//   
imagefill($image,0,0,$white);//       
그리고 인증 코드 가 실현 되 기 전에 빈 변 수 를 설명 하여 인증 코드 를 저장 합 니 다.

$session = ""; //    ,      
for($i=0;$i<4;$i++){ 
  $size = 6; 
  $x = $i*25+mt_rand(5,10); 
  $y = mt_rand(5,10); 
  $sizi_color = imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220)); 
  $char = join("",array_merge(range('a','z'),range('A','Z'),range(0,9))); 
  $char = str_shuffle($char); 
  $char = substr($char,0,1); 
  imagestring($image,$size,$x,$y,$char,$sizi_color); 
  $session .= $char ; //               
} 
  $_SESSION['session'] = $session; //                

for($k=0;$k<200;$k++){ 
  $rand_color = imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200)); 
  imagesetpixel($image,mt_rand(1,99),mt_rand(1,29),$rand_color); 
} 
 
for($n=0;$n<5;$n++){ 
  $line_color = imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220)); 
  imageline($image,mt_rand(1,99),mt_rand(1,29),mt_rand(1,99),mt_rand(1,29),$line_color); 
} 
 
header('content-type:image/png');//         
imagepng( $image ); // png    $image   
imagedestroy( $image ); //     

POST 방식 으로 인증 코드 를 받다.strtolower 함 수 는 서버 가 대소 문 자 를 구분 하지 않도록 하 는 것 입 니 다.이렇게 하면 사용자 의 실수 율 을 효과적으로 줄 일 수 있다.

if(isset($_POST['session'])){ 
  session_start(); 
  if(strtolower($_POST['session'])==strtolower($_SESSION['session'])){ 
    echo'<font color="#0000CC">    </form>'; 
  }else{ 
    echo '<font color="#CC0000"><b>    </b></font>'; 
  } 
  exit(); 
} 
다음은 HTML 페이지 코드 입 니 다.

<!DOCTYPE html> 
<html> 
<head> 
 <meta charset="utf-8"/> 
 <title>     </title> 
</head> 
<body> 
  <form method="post" action="./tushu.php"> 
  <p>     :<img id="img" border="1" src="http://localhost//xxx.php" width="100" height="30"></p> 
  <a href="javascript:void(0)" rel="external nofollow" onclick="document.getElementById('img').src='http://localhost//xxx.php'">   ?   </a> 
  <p>          :<input type="text" name="session" value=""/></p> 
  <p><input type="submit" value="  " style="padding:6px 10px;"></p> 
  </form> 
</body> 
</html> 

HTML 코드 에 이벤트 onclick 이 추가 되 었 습 니 다.사용자 가 현재 인증 코드 를 식별 할 수 없 을 때 브 라 우 저 를 새로 고치 지 않 고'잘 보이 지 않 습 니까?'"하 나 를 바 꾸 면 인증 코드 를 바 꿀 수 있 습 니 다."

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기