PHP GD 生成图片验证码+session获取存储验证码

WBOY
Release: 2016-06-13 12:01:32
Original
953 people have browsed it

PHP GD 生成图片验证码+session获取储存验证码

    下面分享一个PHP制作的图片验证码案例:案例如下图:


   运用PHP GD详细请看:http://www.php.net/manual/zh/book.image.php

   后台图片代码如下:

 

<?phpsession_start ();$image = imagecreatetruecolor(100,30);//创建一个宽100,高度30的图片$bgcolor=imagecolorallocate($image,255,255,255);//图片背景是白色imagefill($image,0,0,$bgcolor);//图片填充白色//随机数/**for($i=0;$i<4;$i++){  $fontsize=6;  $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));  $fontcontent=rand(0,9);  $x=($i*100/4)+ rand(5,10);  $y=rand(5,10);  imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);}**///随机数据$captch_code=&#39;&#39;;for($i=0;$i<4;$i++){  $fontsize=6;  $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));  $data=&#39;asdfdfglfg74erf21854hgfhgfhkg4ljkghjtrtywiqpoqpwepdfgvnjytyut12313345645667686797800&#39;;  $fontcontent=substr($data,rand(0,strlen($data)),1);  $captch_code.=$fontcontent;    $x=($i*100/4)+ rand(5,10);  $y=rand(5,10);  imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);}$_SESSION[&#39;authcode&#39;]=$captch_code;//随机点for($i=0;$i<200;$i++){  $pointcolor=imagecolorallocate($image,rand(50,120),rand(50,120),rand(50,120));  imagesetpixel($image,rand(1,99),rand(1,99),$pointcolor);}//随机线for($i=0;$i<3;$i++){  $linecolor=imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));  imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);}header("content-type:image/png");imagepng($image);imagedestory($image);?>
Copy after login

前台代码如下:

<?php if(isset($_REQUEST[&#39;autocode&#39;])){      session_start();	  if(strtolower($_POST[&#39;autocode&#39;]) == $_SESSION[&#39;authcode&#39;]){	   echo &#39;正确&#39;;	  }else{	  echo&#39;错误&#39;;	  }      exit();   }?>     <meta chartset="utf-8">         
Copy after login

验证码图片:PHP GD 生成图片验证码+session获取存储验证码 width="100" style="max-width:90%" />

输入内容:


添加点击执行修改如下即可:

  <p>验证码图片:<img  border="1" id="capthcha_img" onclick="this.src='captcha.php?r='+Math.random()" src="captcha.php?r=" echo rand alt="PHP GD 生成图片验证码+session获取存储验证码" > width="100"    style="max-width:90%"  /> <a href="javascript:void(0)" onclick="document.getElementById('capthcha_img').src='captcha.php?r='+Math.random()">换一个</a></p>
Copy after login


本案例原创,转载请注明!

  

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!