Verification code generation and application examples_PHP tutorial

WBOY
Release: 2016-07-13 17:09:52
Original
802 people have browsed it

验证码生成与应用实例
在写用户验证页面,如注册,登录的时候,为了加强用户登录的安全性,添加验证码验证。
验证码通过gd生成png图片,并把$randval随机数字赋给$_session['login_check_num'],在通过用户输入的$_post进行比较,来判断是否正确。达到需要实现的功能,需要修改php教程.ini文件,使php支持gd库。

//调用此页面,如果下面的式子成立,则生成验证码图片
if($_get["action"]=="verifycode")
{
rand_create();
}
//验证码图片生成
function rand_create()
{
//通知浏览器将要输出png图片
header("content-type: image/png");
//准备好随机数发生器种子
srand((double)microtime()*1000000);
//准备图片的相关参数
$im = imagecreate(62,20);
$black = imagecolorallocate($im, 0,0,0); //rgb黑色标识符
$white = imagecolorallocate($im, 255,255,255); //rgb白色标识符
$gray = imagecolorallocate($im, 200,200,200); //rgb灰色标识符
//开始作图
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("验证码输入错误");
}
}
?>

 

验证码,是一种区分用户是计算机和人的公共全自动程序。在captcha测试中,作为服务器的计算机会自动生成一个问题由用户来解答。这个问题可以由计算机生成并评判,但是必须只有人类才能解答。由于计算机无法解答captcha的问题,所以回答出问题的用户就可以被认为是人类。

*/
session_start();
$string = null;
$im = imagecreatetruecolor(60,25);  //创建真彩图60*25
$bg = imagecolorallocate($im,255,255,255);//白色背景
imagefill($im,0,0,$bg);填充白色
$x = 5;//
$y = 0;//文字坐标
for($i=0 ; $i<4 ;$i++)
{
$char = mt_rand(0,9);
$string .=$char;
$y = mt_rand(0,10);
$ccolor = imagecolorallocate($im,mt_rand(0,230),mt_rand(0,230),mt_rand(0,230));
imagechar($im,6,$x,$y,$char,$ccolor);//填充文字
$x += mt_rand(10,15);
}
for($i=0 ;$i
{
$x1 = mt_rand(0,80);
$x2 = mt_rand(0,80);
$y1 = mt_rand(0,30);
$y2 = mt_rand(0,30);
$x2 = $x1 +mt_rand(1,5);
$y2 = $y1 +mt_rand(1,5);
$lc = imagecolorallocate($im,mt_rand(0,230),mt_rand(0,230),mt_rand(0,230));
imageline($im,$x1,$y1,$x2,$y2,$lc);//填充线条
}
$_session['code'] = md5($string);
header("content-type:image/jpeg");
imagepng($im);
imagedestroy($im);

更多详细内容请查看:http://www.bKjia.c0m/phper/php-cy/33707.htm


当然我们也可以能过同时我们可以明知php ajax来实例验证功能。

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629715.htmlTechArticleVerification code generation and application examples When writing user verification pages, such as registration and login, in order to strengthen user login Security, add verification code verification. The verification code generates png images through gd...
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