①?新建code.php验证码生成文件
php
session_start();
//生成验证码图片
Header("Content-type: image/PNG");
imagefill($im,0,0,$back); //背景
srand((double)microtime()*1000000);
$vcodes.=$authnum;
}
for($i=0;$i100;$i++) //加入干扰象素
{
imagesetpixel($im, rand()p , rand()0 , $randcolor);
ImagePNG($im);
ImageDestroy($im);
$_SESSION['Checknum'] = $vcodes;
② 显示验证码图片
在需要显示验证码的页面中加入
input type="text" name="passcode"
img src="code.php"
③判断并获取验证码的值
验证码是通过第一步骤代码中的$_SESSION['Checknum'] = $vcodes;赋的值,所以验证码的值存在$_SESSION['Checknum']当中.在验证页面,使用以下代码,
...
session_start();//启动会话
$code=$_POST["passcode"];
if( $code == $_SESSION["Checknum"])
{...}即可完成验证码登录.
运行截图:
望采纳,谢谢
//验证码类
class?ValidateCode?{
private?$code;//验证码
private?$img;//图形资源句柄
private?$font;//指定的字体
private?$fontcolor;//指定字体颜色
//构造方法初始化
public?function?__construct()?{
$this-font?=?dirname(__FILE__).'/font/elephant.ttf';//注意字体路径要写对,否则显示不了图片
//生成随机码
private?function?createCode()?{
$_len?=?strlen($this-charset)-1;
for?($i=0;$i$this-codelen;$i++)?{
$this-code?.=?$this-charset[mt_rand(0,$_len)];
//生成背景
private?function?createBg()?{
$this-img?=?imagecreatetruecolor($this-width,?$this-height);
imagefilledrectangle($this-img,0,$this-height,$this-width,0,$color);
//生成文字
private?function?createFont()?{
$_x?=?$this-width?/?$this-codelen;
//生成线条、雪花
private?function?createLine()?{
//线条
imageline($this-img,mt_rand(0,$this-width),mt_rand(0,$this-height),mt_rand(0,$this-width),mt_rand(0,$this-height),$color);
//雪花
for?($i=0;$i100;$i++)?{
//输出
private?function?outPut()?{
header('Content-type:image/png');
imagepng($this-img);
imagedestroy($this-img);
//对外生成
public?function?doimg()?{
$this-createBg();
$this-createCode();
$this-createLine();
$this-createFont();
$this-outPut();
//获取验证码
public?function?getCode()?{
return?strtolower($this-code);
首先,当用户打开页面时随机产生一个session,然后根据这个值生成验证码图片.
第二,将验证码图片显示到表单上.
第三,当用户提交时表单时,比较session里的值与表单中验证码的值进行比较.
简单的实现过程:
复杂的验证码图片生成:
验证码在表单实现越来越多了,但是用js的写的验证码,总觉得不方便,所以学习了下php实现的验证码.好吧,其实是没有事情干,但是又不想浪费时间,所以学习了下php实现验证码.正所谓,技多不压身.而且,也可以封装成一个函数,以后使用的时候也是很方便的,当然现在未封装.
现在来说说简单的纯数字验证码吧.
如果是初学者,建议按照我代码的注释 //数字 一步步来.最简单的方法,还是把整个代码复制走了.
新建一个captcha.php:
php //10设置session,必须处于脚本最顶部
imagefill($image, 0, 0, $bgcolor); //10设置变量
//设置字体颜色,随机颜色
//设置数字
$captcha_code .= $fontcontent;
//设置坐标
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
} //10存到session
//imagesetpixel — 画一个单一像素
imagedestroy($image);
接着就是静态页的代码了:index.html
doctype htmlhtml
head
title确认验证码title
body
form method="post" action="./form.php"
p
P请输入验证码:input type="text" name='authcode' value=''/p
bodyhtml
从index.html可以看到,提交的表单是到form.php的,所以还要有一个判断的form.php代码:
//isset()检测变量是否设置
if(isset($_REQUEST['authcode'])){ session_start(); //strtolower()小写函数
if(strtolower($_REQUEST['authcode'])== $_SESSION['authcode']){ //跳转页面
echo "script language=\"javascript\""; echo "document.location=\"./form.php\""; echo "/script";
}else{ //提示以及跳转页面
echo "script language=\"javascript\""; echo "alert('输入错误!');"; echo "document.location=\"./form.php\""; echo "/script";
} exit();
废话不多说了,拉代码吧.
//设置需要随机取的值,去掉容易出错的值如0和o
$fontcontent = substr($data, rand(0,strlen($data)),1); //10.=连续定义变量
其他的两个页面,不许要修改.
一般而言,现在就已经够用了.但是就像动漫一样,总会有番外.
那么,我们来个汉字的番外吧.其实我也准备将汉字的验证码放到我的毕业设计里面,虽然现在很流行滑动验证码,但是本人毕竟不是专门学习js的.
php //11设置session,必须处于脚本最顶部
session_start(); //1设置验证码图片大小的函数
//11
//随机选取中文
$in = rand(0,count($strdb)); $cn = $strdb[$in]; //将中文记录到将保存到session的字符串中
$captcha_code .= $cn; /*imagettftext (resource $image ,float $size ,float $angle ,int $x ,int $y,int $color,
string $fontfile ,string $text ) 幕布 ,尺寸,角度,坐标,颜色,字体路径,文本字符串
mt_rand()生成更好的随机数,比rand()快四倍*/
} //11存到session
} //10增加干扰元素,设置线
其他的页面也是不需要修改的.
效果图如下:
以上就是土嘎嘎小编为大家整理的php验证码的简单例子相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!