码迷,mamicode.com
首页 > 其他好文 > 详细

登录页面动态验证码的设置

时间:2017-01-09 21:54:56      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:分享   获取   对象   内部使用   前端   ret   黑色边框   完整   rectangle   

登录页面动态验证码的设置

采用php中创建对象的思想进行动态验证码的设置

1、创建出一个背景图片,用来存放动态码输出位置

 1       function createImage(){
 2         // 创建图片对象,并设置图片的宽高 imagecreatetruecolor
 3         $this->image = imagecreatetruecolor($this->width, $this->height);
 4         // 图片创建背景颜色
 5         // rand(下界,上界), php中的随机数
 6         $backgroundColor = imagecolorallocate($this->image, rand(225,255), rand(225, 255), rand(225,255));
 7         // 填充背景颜色
 8         imagefill($this->image, 0, 0, $backgroundColor);
 9         // 给验证码设置边框
10         $borderColor = imagecolorallocate($this->image, 0, 0 , 0); // 黑色边框
11         // 绘制边框
12         imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $borderColor);
13       }

2、设置动态码中的干扰信息,可以为(点, 线·······)

 

function setDisturbElement(){
        // 随机生成干扰项的数量
        $lineNumbers = rand(2, 4);
        // 通过for循环循环生成每个干扰线
        for ($i=0; $i < $lineNumbers; $i++) {
          // 左上角到右下角
          $x1 = rand(2, $this->width / 2);
          $y1 = rand(2, $this->height / 2);
          $x2 = rand($this->width/2, $this->width-2);
          $y2 = rand($this->height/2, $this->height-2);
          $color = imagecolorallocate($this->image, rand(100, 200), rand(100, 200), rand(100, 200));
          // 绘制线条
          imageline($this->image, $x1, $y1, $x2, $y2, $color);
        }
      }

 

 

 

 

3、生成验证码字符串中的字符串信息,并将其存储到本地的session中以便于信息的验证

 

 1 private function createCheckingCode(){
 2         $code = "0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDASZXCVBNM";
 3         // echo $code;
 4         // 接受验证码的字符串
 5         $string = ‘‘;
 6         for ($i=0; $i < $this->codeNumbers; $i++) {
 7             $index = rand(0, (strlen($code) - 1));
 8             // echo $index;
 9             $string .= substr($code, $index, 1); // 获取code字符串的下标为$index的字符,添加到String字符串中
10         }
11         return $string;
12       }
13       // 为图像设置验证码文本
14       function setValidationCode(){
15         for ($i=0; $i < $this->codeNumbers; $i++) {
16             $x = rand(1, 4) + $this->width * $i / $this->codeNumbers;
17             $y = rand(1, $this->height/4);
18             $color= imagecolorallocate($this->image, rand(0,128), rand(0, 128), rand(0, 128));
19             $fontSize = rand(6, 8);
20             // 绘制文本
21             imagestring($this->image,$fontSize, $x, $y, $this->checkingCode[$i], $color);
22         }
23       }
24       // 存储验证码到session中
25       function saveVcodeToSession(){
26           include_once(‘session.php‘);
27           $session  = new Session();
28           $session->set(‘checkingCode‘, $this->checkingCode, 10);
29       }

 

 

 

 

 

4、对生成的验证码进行前端输出

 

1 function outputImage(){
2         $this->createImage(); // 绘制图片
3         $this->setDisturbElement(); // 绘制线条
4         $this->setValidationCode();  // 生成文本
5         header("Content-Type:image/png"); // 设置文档输出类型, png类型 | jpg
6         imagepng($this->image);
7       }

 

 

完整代码

 

技术分享
 1 <?php
 2     // 要将验证码的码值保存在session,方便前端进行比对验证
 3     session_start();
 4     /**
 5      * 验证码生成类
 6      */
 7     class Validationcode
 8     {
 9       // private ,私有成员,只限于内部使用
10       private $width;
11       private $height;
12       private $codeNumbers; // 验证码的个数
13       private $image; // 将来要输出的图像
14       private $checkingCode; // 验证码字符串,将被保存在session数组中
15       function __construct($width = 80, $height = 30, $codeNumbers = 4) // 如果不提供参数,则80, 30, 4 为默认值
16       {
17           $this->width        = $width;
18           $this->height       = $height;
19           $this->codeNumbers  = $codeNumbers;
20           $this->checkingCode = $this->createCheckingCode(); // 随机生成验证码
21           // echo $this->checkingCode;
22       }
23       // 创建背景图片函数
24       function createImage(){
25         // 创建图片对象,并设置图片的宽高 imagecreatetruecolor
26         $this->image = imagecreatetruecolor($this->width, $this->height);
27         // 图片创建背景颜色
28         // rand(下界,上界), php中的随机数
29         $backgroundColor = imagecolorallocate($this->image, rand(225,255), rand(225, 255), rand(225,255));
30         // 填充背景颜色
31         imagefill($this->image, 0, 0, $backgroundColor);
32         // 给验证码设置边框
33         $borderColor = imagecolorallocate($this->image, 0, 0 , 0); // 黑色边框
34         // 绘制边框
35         imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $borderColor);
36       }
37       // 设置干扰项
38       function setDisturbElement(){
39         // 随机生成干扰项的数量
40         $lineNumbers = rand(2, 4);
41         // 通过for循环循环生成每个干扰线
42         for ($i=0; $i < $lineNumbers; $i++) {
43           // 左上角到右下角
44           $x1 = rand(2, $this->width / 2);
45           $y1 = rand(2, $this->height / 2);
46           $x2 = rand($this->width/2, $this->width-2);
47           $y2 = rand($this->height/2, $this->height-2);
48           $color = imagecolorallocate($this->image, rand(100, 200), rand(100, 200), rand(100, 200));
49           // 绘制线条
50           imageline($this->image, $x1, $y1, $x2, $y2, $color);
51         }
52       }
53       // 对创建的验证码图片进行输出
54       function outputImage(){
55         $this->createImage(); // 绘制图片
56         $this->setDisturbElement(); // 绘制线条
57         $this->setValidationCode();  // 生成文本
58         header("Content-Type:image/png"); // 设置文档输出类型, png类型 | jpg
59         imagepng($this->image);
60       }
61       // 生成验证码字符串
62       // 私有的函数
63       private function createCheckingCode(){
64         $code = "0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDASZXCVBNM";
65         // echo $code;
66         // 接受验证码的字符串
67         $string = ‘‘;
68         for ($i=0; $i < $this->codeNumbers; $i++) {
69             $index = rand(0, (strlen($code) - 1));
70             // echo $index;
71             $string .= substr($code, $index, 1); // 获取code字符串的下标为$index的字符,添加到String字符串中
72         }
73         return $string;
74       }
75       // 为图像设置验证码文本
76       function setValidationCode(){
77         for ($i=0; $i < $this->codeNumbers; $i++) {
78             $x = rand(1, 4) + $this->width * $i / $this->codeNumbers;
79             $y = rand(1, $this->height/4);
80             $color= imagecolorallocate($this->image, rand(0,128), rand(0, 128), rand(0, 128));
81             $fontSize = rand(6, 8);
82             // 绘制文本
83             imagestring($this->image,$fontSize, $x, $y, $this->checkingCode[$i], $color);
84         }
85       }
86       // 存储验证码到session中
87       function saveVcodeToSession(){
88           include_once(‘session.php‘);
89           $session  = new Session();
90           $session->set(‘checkingCode‘, $this->checkingCode, 10);
91       }
92     }
93     $vcode = new Validationcode();
94     $vcode->outputImage();
95     $vcode->saveVcodeToSession();
96  ?>
php

 

 

 

 

 

 

 

 

登录页面动态验证码的设置

标签:分享   获取   对象   内部使用   前端   ret   黑色边框   完整   rectangle   

原文地址:http://www.cnblogs.com/Asari/p/6266446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!