php生成验证码函数

WBOY
Release: 2016-06-20 13:03:53
Original
860 people have browsed it

下面是php生成验证码的源码:

<p><?php<br /></p>session_start();<br />//session_register('CheckCode'); <br />//PHP4.2以上版本不需要用session_register()注册SESSION变量<br />$type='gif';<br />$width= 45;<br />$height= 20;<br />header("Content-type: image/".$type);<br />srand((double)microtime()*1000000);<br />if(isset($_GET['action'])){<br />	$randval=randStr(4,$_GET['action']);	<br />}else{<br />	$randval=randStr(4,'');	<br />}<br />if($type!='gif'&&function_exists('imagecreatetruecolor')){<br />	$im=@imagecreatetruecolor($width,$height);<br />}else{<br />	$im=@imagecreate($width,$height);<br />}<br />$r=Array(225,211,255,223);<br />$g=Array(225,236,237,215);<br />$b=Array(225,236,166,125);<br />$key=rand(0,3);<br />$backColor=ImageColorAllocate($im,$r[$key],$g[$key],$b[$key]);//背景色(随机)<br />$borderColor=ImageColorAllocate($im,127,157,185);//边框色<br />$pointColor=ImageColorAllocate($im,255,170,255);//点颜色<br />@imagefilledrectangle($im,0,0,$width - 1,$height - 1,$backColor);//背景位置<br />@imagerectangle($im,0,0,$width-1,$height-1,$borderColor); //边框位置<br />$stringColor=ImageColorAllocate($im,255,51,153);<br />for($i=0;$i<=100;$i++){<br />	$pointX=rand(2,$width-2);<br />	$pointY=rand(2,$height-2);<br />	@imagesetpixel($im,$pointX,$pointY,$pointColor);<br />}<br />@imagestring($im,5,5,1,$randval,$stringColor);<br />$ImageFun='Image'.$type;<br />$ImageFun($im);<br />@imagedestroy($im);<br />$_SESSION['CheckCode']=$randval;<br />function randStr($len=6,$format='ALL'){<br />	switch($format){<br />		case 'ALL'://生成包含数字和字母的验证码<br />			$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;<br />		case 'CHAR'://仅生成包含字母的验证码<br />			$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;<br />		case 'NUMBER'://仅生成包含数字的验证码<br />			$chars='0123456789'; break;<br />		default :<br />			$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;<br />	}<br />	$string='';<br />	while(strlen($string)<$len)<br />	$string.=substr($chars,(mt_rand()%strlen($chars)),1);<br />	return $string;<br /><p>}</p>
Copy after login

该函数的具体使用方法请看如下这个示例(这里是生成全数字的验证码):

<img  src="checkCode.php?action=NUMBER"    style="max-width:90%"  style="max-width:90%" / alt="php生成验证码函数" >
Copy after login

相关文章

PHP中文验证码实现代码

Securimage PHP生成验证码类


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