使用PHP制作验证码
2018-02-11 01:34
288 查看
制作验证码的几个步骤:
1.绘制验证码图片
2.将验证码保存到服务器
3.检验用户提交的验证码是否正确
1.绘制验证码:
第一步绘制验证码就这样完成了。
2.将验证码保存在服务器中:
使用了session
3.验证:
1.绘制验证码图片
2.将验证码保存到服务器
3.检验用户提交的验证码是否正确
1.绘制验证码:
$image = imagecreatetruecolor(100,30);//建立一个宽100,高30的画布,且默认背景为黑色 $color = imagecolorallocate($image, 255, 255, 255);// 为一幅图像分配颜色,后三个参数为RGB值 imagefill($image, 0, 0, $color);//将刚才设置的颜色,白色,填充到画布中 for($i = 0; $i < 4; $i++){//验证码的四个数字或字母 $size = 6; $color = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); $data = 'qwertyupasdfghjkzxcvbnm3456789'; $content = substr($data,rand(0,strlen($data)),1); $x = ($i * 100 / 4 ) + rand(5,10); $y = rand(5,15); imagestring($image, $size, $x, $y, $content, $color);//水平地画一行字符串,$x,$y分别为横坐标和纵坐标 } for($i = 0; $i < 300; $i++){//向画布中添加点增加干扰 $color = imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200)); imagesetpixel($image, rand(1,99), rand(1,29), $color);//画一个单一像素 } for($i = 0; $i < 3; $i++){//向画布中添加线增加干扰 $color = imagecolorallocate($image, rand(80,220), rand(80,220), rand(80,220)); imageline($image, rand(1,99), rand(1,29), rand(1,99), rand(1,29), $color);// 画一条线段 } header("Content-type:image/png");//表示输出的是图片类型 imagepng($image); imagedestroy($image);
第一步绘制验证码就这样完成了。
2.将验证码保存在服务器中:
使用了session
session_start(); $image = imagecreatetruecolor(100,30); $color = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $color); $code=''; for($i = 0; $i < 4; $i++){ $size = 6; $color = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); $data = 'qwertyupasdfghjkzxcvbnm3456789'; $content = substr($data,rand(0,strlen($data)),1); $code .= $content; $x = ($i * 100 / 4 ) + rand(5,10); $y = rand(5,15); imagestring($image, $size, $x, $y, $content, $color); } $_SESSION['code'] = $code; for($i = 0; $i < 300; $i++){ $color = imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200)); imagesetpixel($image, rand(1,99), rand(1,29), $color); } for($i = 0; $i < 3; $i++){ $color = imagecolorallocate($image, rand(80,220), rand(80,220), rand(80,220)); imageline($image, rand(1,99), rand(1,29), rand(1,99), rand(1,29), $color); } header("Content-type:image/png"); imagepng($image); imagedestroy($image);
3.验证:
if(isset($_REQUEST['authcode'])){ session_start(); if(strtolower($_REQUEST['authcode']) == $_SESSION['authcode']){ echo "<script>alert('输入正确');window.location.href='form.php';</script>"; }else{ echo "<script>alert('输入错误');window.location.href='form.php';</script>"; } exit(); }
相关文章推荐
- php使用GD库制作验证码,点击验证码或看不清会刷新验证码
- 使用php制作验证码技术
- PHP使用GD库制作验证码的方法(点击验证码或看不清会刷新验证码)
- PHP学习笔记之使用GD库制作验证码(一)
- 使用PHP制作一个验证码
- 使用php制作验证码
- 使用PHP制作新闻系统的思路
- 使用PHP实现登陆验证码
- PHP简单演示如何使用模板制作静态页面
- 一分钟学会使用PHP生成网页验证码
- 像Youtube一样的flv视频网站制作 使用Flex和PHP创建自己的视频应用
- 使用php生成验证码
- PHP中验证码的使用
- php 使用GD库生成验证码
- php 使用GD库生成验证码
- php图片验证码的制作(高级篇)
- 使用PHP JavaScript制作AJAX搜索引擎
- 验证码的制作和使用(ASP)
- 验证码的制作和使用(ASP)
- php图片验证码的制作(基础篇)