PHP生成圖片驗證碼demo【OOP面向對象版本】

来源:http://www.cnblogs.com/zymNoteBook/archive/2016/10/06/5934329.html
-Advertisement-
Play Games

下麵是我今天下午用PHP寫的一個生成圖片驗證碼demo,僅供參考。這個demo總共分為4個文件,具體代碼如下: ...


下麵是我今天下午用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          <!--
 9              * @Description  網站登錄/註冊驗證碼生成類
10              * @Author  趙一鳴
11              * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
12              * @Date  2016年10月6日 
13          -->
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
/**
 * @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 '<script type="text/javascript">
                    alert("驗證碼填寫成功");
                    history.go(-1);
                </script>';
        }else{
            echo '<script type="text/javascript">
                    alert("驗證碼填寫失敗");
                    history.go(-1);
                </script>';
        }
    }

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

轉載請註明出處!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • iOS 10 新規定,在取用相機,相簿,聯絡資訊,麥克風需要在 Info.plist 加入指定的 key,否則閃退: Info.plist ...
  • Netbeans 8.2在這個國慶期間終於發佈了,其與PHP相關的新特性主要有: 支持PHP 7 詳見前面翻譯的一篇文章: "Netbeans 8.2將支持PHP 7" 編輯器功能增強 文檔好像沒有明確說明,我也還沒有發現。 PHP項目支持自定義註解 操作如下圖: 然後,當你在編寫代碼註解時,就可以 ...
  • C++類中的虛表結構是C++對象模型中一個重要的知識點,這裡咱們就來深入分析下虛表的在記憶體中的結構。 C++一個類中有虛函數的話就會有一個虛表指針,其指向對應的虛表,一般一個類只會有一個虛表,每個虛表有多個”插槽”,每個插槽存放一個虛函數的地址。插槽中的內容可以被覆蓋,子類如果重寫了父類中的虛函數, ...
  • 代碼如下: import java.util.Enumeration; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import j... ...
  • For collecting and analyzing data. 【啟示】本處所分享的內容均是筆者從一些專業書籍中學習所得,也許會有一些自己使用過程中的技巧、心得、小經驗一類的,但遠比不上書中所講述的精彩翔實。只因自己在學習過程中深感在R爬蟲應用中互聯網可搜索的公開資源並不如其它知識豐富,特此稍 ...
  • 博主python菜鳥,本想在win7下安裝一個pyquery玩玩爬蟲,折騰了好幾天終於搞好了,發現python這坑不是一般的深啊。 有一部分沒有截圖,請諒解 python版本3.4 1.下載easy_install和pip,這步跳過,python 3.X預設自帶 2.嘗試用pip pyquery i ...
  • 說明:在我們調試C語言的過程中,經常會遇到duplicate symbol錯誤(在Mac平臺下利用Xcode集成開發環境)。如下圖: 一.簡單分析一下C語言程式的開發步驟。 由上圖我們可以看出C語言由編寫源程式->編譯->鏈接->運行幾個步驟進行。 編寫源程式: C語言的源文件的擴展名為.c,源文件 ...
  • 批處理是一次性向資料庫發出多條查詢指令,一起執行Statement 介面定義的方法:|—增加批處理語句: |—執行批處理: PreparedStatement介面定義的方法:增加批處理:public void addBatche()throws SQLexceeption public class ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...