PHP实现验证码的实现
来源:互联网 发布:java 捕获异常后抛出 编辑:程序博客网 时间:2024/06/03 12:28
利用PHP依赖的GD库可以轻松实现验证码
验证码生成步骤:
1、生成底图
利用imagecreatetruecolor()方法生成底图。
函数解释:
imagecreatetruecolor — 新建一个真彩色图像
说明:
resource imagecreatetruecolor ( int
imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。
imagecolorallocate — 为一幅图像分配颜色 int imagecolorallocate( resource
说明:
imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。
$image = imagecreatetruecolor(100,30); //生成一张100*30px大小的图片 $bgcolor = imagecolorallocate($image,255,255,255); //改变底色 白色 imagefill( $image,0,0,$bgcolor); //填充底色
2、生成验证内容
利用随机数配合循环进行随机验证码的生成(包括数字验证码以及字幕数字混合验证码)
$capth_code = ''; for($i=0;$i<4;$i++) //生成字母 数字混合体验证码 { $fontsize = 6; $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120)); //字符颜色为深色 $data = 'abcdefghigklmeopqrstuvwxyz123456789'; $fontcontent = substr($data,rand(0,strlen($data)),1); $capth_code .= $fontcontent; $x = ($i*100/4) + rand(5,10); //x轴位置 $y = rand(5,10); //y轴位置 imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); } $_SESSION['authcode'] = $capth_code;
3、生成验证码图片
利用imagepng生成图片(当然还有其他样式可选)
函数解释:
imagepng — 以 PNG 格式将图像输出到浏览器或文件
说明
bool imagepng ( resource
imagepng() 将 GD 图像流(image)以 PNG 格式输出到标准输出(通常为浏览器),或者如果用 filename 给出了文件名则将其输出到该文件。
header( 'content-type: image/png' ); //一定需要提前输出图片的header信息 imagepng( $image );
4、校验验证内容
利用PHP中的session会话变量进行验证码的保存和校验。
以下代码的实现首先要依赖于PHP中已经安装有GD库。所以在使用下列代码时需要验证你的PHP是否已经集成了GD库。可利用phpinfo()查看。
<?php/** * Author: helen * CreateTime: 2015/9/22 19:54 * description:验证码的制作 */ session_start(); $image = imagecreatetruecolor(100,30); //生成一张100*30px大小的图片 $bgcolor = imagecolorallocate($image,255,255,255); //改变底色 白色 imagefill( $image,0,0,$bgcolor); //填充底色 /*for($i=0;$i<4;$i++) //生成随机四位数字 { $fontsize = 6; $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120)); //字符颜色为深色 $fontcontent = rand(0,9); $x = ($i*100/4) + rand(5,10); //x轴位置 $y = rand(5,10); //y轴位置 imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); }*/ $capth_code = ''; for($i=0;$i<4;$i++) //生成字母 数字混合体验证码 { $fontsize = 6; $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120)); //字符颜色为深色 $data = 'abcdefghigklmeopqrstuvwxyz123456789'; $fontcontent = substr($data,rand(0,strlen($data)),1); $capth_code .= $fontcontent; $x = ($i*100/4) + rand(5,10); //x轴位置 $y = rand(5,10); //y轴位置 imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); } $_SESSION['authcode'] = $capth_code; for($i=1;$i<200;$i++) //生成点干扰元素 { $pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200)); imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor); } for($i=0;$i<3;$i++) //生成线干扰元素 { $linecolor = imagecolorallocate($image,rand(80,200),rand(80,200),rand(80,200)); imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor); } header( 'content-type: image/png' ); //一定需要提前输出图片的header信息 imagepng( $image ); //脚本结束,清除验证码图片 imagedestroy( $image );?>
- PHP验证码的实现
- php验证码的实现
- PHP验证码的实现
- PHP的验证码实现
- PHP实现验证码的实现
- 【php】php验证码的简单实现
- php实现验证码
- PHP实现验证码
- PHP实现验证码
- PHP实现验证码
- php实现验证码
- php实现验证码
- PHP实现验证码
- PHP---验证码实现
- php验证码实现
- 【PHP】验证码实现
- php实现验证码
- PHP验证码实现
- Integer、int类型的==关系
- hanoi问题
- ROS(Robot Operating System) 基础
- VS2013 好用的插件
- unity3d实现Loading进度条异步加载场景
- PHP实现验证码的实现
- 如何调试和分析Luci
- python随笔1: 模块的安装方式总结
- Alpha、Beta、RC、GA版本的区别
- 在OpenWrt路由器上添加LuCI的模块
- HttpSessionListener案例: 自定义HttpSession扫描器
- Valid Palindrome
- javascript 显示一定范围内的素数(质数)【转】
- 对话管理