0

0

PHP生成图片验证码demo【OOP面向对象版本】

php中文网

php中文网

发布时间:2016-10-08 16:04:44

|

1331人浏览过

|

来源于php中文网

原创

下面是我今天下午用php写的一个生成图片验证码demo,仅供参考。

这个demo总共分为4个文件,具体代码如下:

1、code.html中的代码:

 1 doctype html>
 2 <html lang="en">
 3     <head>
 4         <meta charset="utf-8" />
 5         <title>登录、注册验证码生成title>
 6     head>
 7     <body>
 8          
14         <form action="checkcode.php" method="post">
15             <input type="text" name="code" /><br/>
16             <img src="showcode.php" onclick="this.setAttribute('src','showcode.php?'+Math.random())" />
17             <span>看不清?点击图片即可切换验证码span><br/>
18             <input type="submit" name="sub" value="登录/注册" />
19         form>
20     body>
21 html>

2、createcode.class.php中的代码:

 1 php
 2     /**
 3      * @Description  网站登录/注册验证码生成类
 4      * @Author  赵一鸣
 5      * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
 6      * @Date  2016年10月6日 
 7      */
 8     class Createcode{
 9         //画布资源
10         public $img;
11         //画布宽度
12         private $img_width;
13         //画布高度
14         private $img_height;
15         //画布颜色
16         private $img_bgcolor;
17         //验证码文字内容
18         private $str_content;
19         //生成的验证码内容
20         private $code_content;
21         //验证码颜色
22         private $code_content_color;
23         //构造函数
24         public function __construct($img_width,$img_height,$str_content,$code_content_color){
25             if($this->gdcheck()){
26                 $this->img_width = $img_width;
27                 $this->img_height = $img_height;
28                 $this->str_content = $str_content;
29                 $this->code_content_color = $code_content_color;
30                 $this->get_code();
31                 $this->session_code();
32             }
33         }
34         //生成画布
35         public function get_img(){
36             //定义画布
37             $this->img = imagecreatetruecolor($this->img_width, $this->img_height);
38             //画布背景色
39             $this->img_bgcolor = imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
40             //给画图填充背景色
41             imagefill($this->img, 0, 0, $this->img_bgcolor);
42             //取得画布的宽高
43             $img_width = imagesx($this->img);
44             $img_height = imagesy($this->img);
45             //画布中插入验证码
46             imagestring($this->img, 5, ($this->img_width/3), ($this->img_height/2.5), $this->code_content, imagecolorallocate($this->img, hexdec(substr($this->code_content_color, 1,2)), hexdec(substr($this->code_content_color, 3,2)), hexdec(substr($this->code_content_color, 5,2))));
47             //画布中插入像素点
48             $this->get_pix();
49             //画布中插入直线
50             $this->get_line();
51             //画布显示
52             header('Content-type:image/png');
53             imagepng($this->img);
54         }
55         //生成验证码
56         private function get_code(){
57             $str_content_len = strlen($this->str_content);
58             for($i=0;$i<4;$i++){
59                 $this->code_content .= substr($this->str_content, mt_rand(0,$str_content_len-1),1);
60             }
61         }
62         //生成像素点
63         private function get_pix(){
64             for($j=0;$j<300;$j++){
65                 $image_pix .= imagesetpixel($this->img, mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)));
66             }
67             return $image_pix;
68         }
69         //生成直线
70         private function get_line(){
71             for($l=0;$l<2;$l++){
72                 $img_line .= imageline($this->img, mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)));
73             }
74             return $img_line;
75         }
76         //session存储验证码
77         private function session_code(){
78             session_start();
79             $_SESSION['code'] = $this->code_content;
80         }
81         //判断程序是否支持GD库
82         private function gdcheck(){
83             if(extension_loaded('gd')){
84                 return true;
85             }else{
86                 return false;
87                 exit();
88             }
89         }
90     }

3、checkcode.php中的代码:

立即学习PHP免费学习笔记(深入)”;

Live PPT
Live PPT

一款AI智能化生成演示内容的在线工具。只需输入一句话、粘贴一段内容、或者导入文件,AI生成高质量PPT。

下载
php
/**
 * @Description  网站登录/注册验证码生成类
 * @Author  赵一鸣
 * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
 * @Date  2016年10月6日
 */
    header('Content-type:text/html;charset="utf-8"');
    session_start();
    if($_POST['code']!=''){
        if($_SESSION['code']==$_POST['code']){
            echo '';
        }else{
            echo '';
        }
    }

4、showcode.php中的代码:

 1 php
 2 /**
 3  * @Description  网站登录/注册验证码生成类
 4  * @Author  赵一鸣
 5  * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
 6  * @Date  2016年10月6日
 7  */
 8     function __autoload($classname){
 9         include strtolower($classname).'.class.php';
10     }
11     //定义验证码的取值范围
12     $str_content = 'abcdefghijklmnopqrstuvwxyz0123456789';
13     //验证码文字颜色
14     $code_content_color = '#ffffff';
15     //初始化对象
16     $code = new Createcode(100,30,$str_content,$code_content_color);
17     $code->get_img();

原文地址:http://www.zymseo.com/php/334.html

转载请注明出处!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

4

2026.01.16

全民K歌得高分教程大全
全民K歌得高分教程大全

本专题整合了全民K歌得高分技巧汇总,阅读专题下面的文章了解更多详细内容。

3

2026.01.16

C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

10

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

33

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

15

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

42

2026.01.15

浏览器缓存清理方法汇总
浏览器缓存清理方法汇总

本专题整合了浏览器缓存清理教程汇总,阅读专题下面的文章了解更多详细内容。

7

2026.01.15

ps图片相关教程汇总
ps图片相关教程汇总

本专题整合了ps图片设置相关教程合集,阅读专题下面的文章了解更多详细内容。

9

2026.01.15

ppt一键生成相关合集
ppt一键生成相关合集

本专题整合了ppt一键生成相关教程汇总,阅读专题下面的的文章了解更多详细内容。

6

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号