<?php
session_start(); //在文件的最开头打开session
//把英文和中文分别放入两个数组中
$english_arr=array('q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j',
'k','l','z','x','c','v','b','n','m');
$chinese_arr=array('一','二','三','四','五','六','七','八','九','十','零','木','和','我',
'人','有','的','右','主','产','为','这','工','上','下','左');
/**
* 取得中文和英文
*/
/**
* 取得一个随机数, 随机字母,随机中文
*/
for($i=0;$i<5;$i++)
{
$index=rand(0,2);
if($index=='0')
{
$num.=rand(0,9);
// echo "<font color=red>$num</font><br>";
}
else if($index=='1')
{
$num.=$english_arr[rand(0,25)];
// echo "<font color=blue>$num</font><br>";
}
else if($index=='2')
{
// echo $num;
$num.=$chinese_arr[rand(0,25)]; //如果加入的是中文,则要转换编码
// echo "<font color=green>$num</font><br>";
}
}
$_SESSION[check_num]=$num; //把这个验证码记入session中
//echo $_SESSION[check_num];
// p_w_picpathcreatetruecolor(宽,高)
$p_w_picpath=p_w_picpathcreatetruecolor(150,50); //创建一个100,30的图片
// p_w_picpathcolorallocate(图片名,红,蓝,绿) 三原色
p_w_picpathcolorallocate($p_w_picpath,rand(0,255),rand(0,255),rand(0,255));//第一次是设置图片背景色,三原色随机
$color=p_w_picpathcolorallocate($p_w_picpath,255,255,255);//以后的为了创建一种颜色
// p_w_picpathstring(图片名,字体类型1-6六种,X坐标,Y坐标,内容,颜色)
//p_w_picpathstring($p_w_picpath,rand(3,6),10,3,$num,$color); //把内容写到图片中 不能输出中文
// p_w_picpathttftext(图片名,字体大小,偏移角度,X坐标,Y坐标,颜色,字体文件地址,输入的内容) 这个内容必须要在输入的时候转换编码
p_w_picpathttftext($p_w_picpath,rand(15,18),0,rand(2,50),rand(30,39),$color,'simkai.ttf',iconv('GBK','UTF-8',$num)); //把内容写到图片中,可以有中文
/**
* 画线和点做干扰
*/
for($i=0;$i<10;$i++)
{
$linecolor=p_w_picpathcolorallocate($p_w_picpath,rand(0,255),rand(0,255),rand(0,255));
p_w_picpathline($p_w_picpath,rand(0,150),rand(0,50),rand(0,150),rand(0,50),$linecolor);
}
for($i=0;$i<200;$i++)
{
$pixelcolor=p_w_picpathcolorallocate($p_w_picpath,rand(0,255),rand(0,255),rand(0,255));
p_w_picpathsetpixel($p_w_picpath,rand(0,150),rand(0,50),$pixelcolor);
}
/**
* 输出图片
*/
//echo dechex(rand(0,15));
header("ContentType:p_w_picpath/jpeg"); //设置此文件是以什么形势输出 这里是作为一张图片
p_w_picpathjpeg($p_w_picpath); //输出jpeg格式的图片
?>
PHP验证码代码分享
原创
©著作权归作者所有:来自51CTO博客作者mhxj1000的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
自动识别网页图形验证码
Python自动识别网页图形验证码
验证码 预处理 Selenium -
生成验证码-超简单
轻松应对验证挑战:简单验证码的实现方法!
验证码 Java -
VC显示网页验证码
效果截图:
验证码 获取图片 缓存 -
网页验证码简单实现
在这个地方我简单的做了一个网页验证码的实现,这个验证是对应的四位的数字,是由随机
验证码 网页 html ide