php编程——验证码的实现(session方法)
2016-06-05 22:31
585 查看
index.php(实现输入验证码页面)代码如下:
<html>
<head>
<title>check code</title>
</head>
<body>
<form name=check method=post action=check.php>
<input type=hidden name=init value=1>
验证码:<input type=text name=code maxlength=4 style="width=50px;">
<!--得到验证码图片-->
<img src=image.php>
<p>
<input type=submit value="提交">
</form>
</body>
</html>
image.php(验证码生成页面)代码如下:
<?php
session_start();
srand((double)microtime()*1000000);
$authnum=rand(1000,9999);
session_register("authnum");
header("content-type:image/png");
function creat_image($width,$height,$authnum)
{
srand((double)microtime()*1000000);
$im = imagecreate($width,$height);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,0,0,$gray);
//将四位整数验证码绘入图片
imagestring($im, 5, 10, 3, $authnum, $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);
}
creat_image(60,20,$authnum);
?>
check.php(验证界面)代码如下:
<?php
session_start();
if(!isset($init)) $init=0;
if($init)
{
if($_POST['code']==$authnum)
{
echo "验证码正确!";
}
else echo "验证码错误,请重新输入!";
}
else echo "调用页面错误!"
?>
<html>
<head>
<title>check code</title>
</head>
<body>
<form name=check method=post action=check.php>
<input type=hidden name=init value=1>
验证码:<input type=text name=code maxlength=4 style="width=50px;">
<!--得到验证码图片-->
<img src=image.php>
<p>
<input type=submit value="提交">
</form>
</body>
</html>
image.php(验证码生成页面)代码如下:
<?php
session_start();
srand((double)microtime()*1000000);
$authnum=rand(1000,9999);
session_register("authnum");
header("content-type:image/png");
function creat_image($width,$height,$authnum)
{
srand((double)microtime()*1000000);
$im = imagecreate($width,$height);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,0,0,$gray);
//将四位整数验证码绘入图片
imagestring($im, 5, 10, 3, $authnum, $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);
}
creat_image(60,20,$authnum);
?>
check.php(验证界面)代码如下:
<?php
session_start();
if(!isset($init)) $init=0;
if($init)
{
if($_POST['code']==$authnum)
{
echo "验证码正确!";
}
else echo "验证码错误,请重新输入!";
}
else echo "调用页面错误!"
?>
相关文章推荐
- yii2分页扩展之实现跳转到具体某页
- PHPExcel 导出表格 不知道好不好用
- 高通8X16的TP笔记(二)
- Slim初体验
- zend framework多模块多Layout配置
- PHP其他系统函数
- php笔记6-函数和递归,递推
- <转>.php导出excel(多种方法)
- PHP的面向对象编程
- PHP-Fpm应用池配置
- XTEA加密,CBC模式的PHP调用方法
- phpcms v9最实用的23个调用代码
- ThinkPHP 知识点链接
- PHP笔记5-流程控制,文件加载,错误处理
- php cookie用法之 setcookie()函数
- php封装练习
- php基础-错误处理
- php 面向对象之封装
- 高通8X16的TP笔记(一)
- 加密后的php代码需要ZendGuardLoader模块才能正常运行