对于一个网站来说,肯定会需要一个提供验证功能的页面。那么我们现在就需要考虑验证页面中的验证码的应用了。我们接下来将要为大家具体讲解有关一、准备一个展示并提交php验证码的页面
php @header(content-type:text/html; charset=utf-8); //打开session session_start(); ?> html> head> meta http-equiv=content-type content=text/html; charset=utf-8 /> title>php验证码示例title> head> body> 验证码:br/> iframe id=iimg height=100 width=300 src=img.php frameborder=0 >iframe> br/> input type=button value=看不清,换一张 onclick=iimg.location.reload();> br> form action=validate.php method=post> 输入验证码:input name=imgid style=width:60> input type=submit value=确定> form> body> html>
二、以下是php验证码生成页面,该页面在第一页面中被调用
php header(content-type: image/gif); session_start(); //验证码为随机字符,以下是算法 $randval; for($i=0;$i7;$i++){ $randstr = mt_rand(ord('a'),ord('z')); srand((double)microtime()*1000000); $randv = mt_rand(0,10); if($randv%2==0){ $randval.=mt_rand(0,10); }else{ $randval.=chr($randstr); } } //注册php验证码到session session_register($randval); //以下是绘制验证码图 $height = 50;//图高 $width = 100;//图宽 $im = imagecreatetruecolor($width, $height); $white = imagecolorallocate($im, 255, 255, 255); $blue = imagecolorallocate($im, 0, 0, 64); imagefill($im, 0, 0, $white); srand((double)microtime()*1000000000); imageline($im, mt_rand(0,$width/3), mt_rand(0,$height/3), mt_rand($width/3,$width), mt_rand($height/3,$height), $blue); srand((double)microtime()*1000000); imageline($im, mt_rand($width/3,$width), mt_rand(0,$height/3), mt_rand(0,$width/3), mt_rand(0,$height/3), $blue); srand((double)microtime()*1000000); imagestring($im,16 , mt_rand(0,$width - strlen($randval) * 10), mt_rand(0,$height-12), $randval, $blue); imagegif($im); imagedestroy($im); /* 需要注意的是:为了支持以上绘图函数,我们必须在php载入gd2图形处理库,可修改 php.ini 文件中的 ;extension=php_gd2.dll 为 extension=php_gd2.dll 即开启gd2库 */ ?>
三、这是验证页面,提示php验证码是否通过验证
php @header(content-type:text/html; charset=utf-8); //开启session session_start(); //得到用户输入的验证码,并转换成大写 $imgid_req = $_request['imgid']; $imgid_req = strtoupper($imgid_req); //验证该字符串是否注册了session变量 if (session_is_registered($imgid_req)) { echo font color=blue >通过验证!font>; } else { echo font color=red >验证错误!font>; } //关闭session,以清除所有注册过的变量 session_destroy(); ?>
http://www.bkjia.com/phpjc/446325.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/446325.htmltecharticle对于一个网站来说,肯定会需要一个提供验证功能的页面。那么我们现在就需要考虑验证页面中的验证码的应用了。我们接下来将要为大家...