在用户验证页面,如注册,登录的时候,为了加强用户登录的安全性,添加验证码验证代码,下面我为各位朋友提供几种不同同类型的php验证代码程序,最后一款是一款使用了验证代码的实例代码,实例代码如下:
- date_default_timezone_set('asia/shanghai');
- function setcode($len)
- {
- $code = '';
- for ($i=0;$i<$len;$i++)
- {
- $code .= chr(drand());
- }
- return $code;
- }
-
- function drand()
- {
- $rand = mt_rand(0,2);
- $str = '';
- switch ($rand)
- {
- case 0: $str = mt_rand(48,57);break;
- case 1: $str = mt_rand(65,90);break;
- case 2: $str = mt_rand(97,122);break;
- }
- return $str;
- }
- $_session['checkcode'] = $code = setcode(5);
-
-
-
- session_start();
- session_register('safecode');
- $type = 'gif';
- $width= 40;
- $height= 16;
- header("content-type: image/".$type);
- srand((double)microtime()*1000000);
- $randval = randstr(4,"");
- if($type!='gif' && function_exists('imagecreatetruecolor')){
- $im = @imagecreatetruecolor($width,$height);
- }else{
- $im = @imagecreate($width,$height);
- }
- $r = array(225,211,255,223);
- $g = array(225,236,237,215);
- $b = array(225,236,166,125);
-
- $key = rand(0,3);
-
- $backcolor = imagecolorallocate($im,$r[$key],$g[$key],$b[$key]);
- $bordercolor = imagecolorallocate($im, 0, 0, 0);
- $pointcolor = imagecolorallocate($im, 255, 170, 255);
-
- @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backcolor);
- @imagerectangle($im, 0, 0, $width-1, $height-1, $bordercolor);
- $stringcolor = imagecolorallocate($im, 255,51,153);
-
- for($i=0;$i<=100;$i++){
- $pointx = rand(2,$width-2);
- $pointy = rand(2,$height-2);
- @imagesetpixel($im, $pointx, $pointy, $pointcolor);
- }
-
- @imagestring($im, 3, 5, 1, $randval, $stringcolor);
- $imagefun='image'.$type;
- $imagefun($im);
- @imagedestroy($im);
- $_session['safecode'] = $randval;
-
- function randstr($len=6,$format='all') {
- switch($format) {
- case 'all':
- $chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789www.111cn.net'; break;
- case 'char':
- $chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; break;
- case 'number':
- $chars='0123456789'; break;
- default :
- $chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789';
- break;
- }
- $string="";
- while(strlen($string)<$len)
- $string.=substr($chars,(mt_rand()%strlen($chars)),1);
- return $string;
- }
-
-
- if($_get["action"]=="verifycode")
- {
- rand_create();
- }
-
- function rand_create()
- {
-
- header("content-type: image/png");
-
- srand((double)microtime()*1000000);
-
- $im = imagecreate(62,20);
- $black = imagecolorallocate($im, 0,0,0);
- $white = imagecolorallocate($im, 255,255,255);
- $gray = imagecolorallocate($im, 200,200,200);
-
- imagefill($im,0,0,$gray);
- while(($randval=rand()%100000)<10000);{
- $_session["login_check_num"] = $randval;
-
- imagestring($im, 5, 10, 3, $randval, $black);
- }
-
- for($i=0;$i<200;$i++){
- $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
- imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
- }
-
- imagepng($im);
-
- imagedestroy($im);
- }
-
- function rand_check()
- {
- if($_post["reg_rand"] == $_session["login_check_num"]){
- return true;
- }
- else{
- exit("验证码输入错误");
- }
- }
验证码通过gd生成png图片,并把$randval随机数字赋给$_session['login_check_num'],在通过用户输入的$_post进行比较,来判断是否正确,达到需要实现的功能,需要修改php.ini文件,使php支持gd库. |