国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 類庫下載 PHP類庫 PHP生成圖片驗證碼demo【OOP面向?qū)ο蟀姹尽?/span>

PHP生成圖片驗證碼demo【OOP面向?qū)ο蟀姹尽?/h1> Oct 09, 2016 am 09:19 AM


PHP生成圖片驗證碼demo【OOP面向?qū)ο蟀姹尽?/p>

下面是我今天下午用PHP寫的一個生成圖片驗證碼demo,僅供參考。

這個demo總共分為4個文件,具體代碼如下:

1、code.html中的代碼:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>登錄、注冊驗證碼生成</title>
    </head>
    <body>
         <!--
             * @Description  網(wǎng)站登錄/注冊驗證碼生成類
             * @Author  趙一鳴
             * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
             * @Date  2016年10月6日 
         -->
        <form action="checkcode.php" method="post">
            <input type="text" name="code" /><br/>
            <img  src="/static/imghw/default1.png"  data-src="showcode.php"  class="lazy"   onclick="this.setAttribute(&#39;src&#39;,&#39;showcode.php?&#39;+Math.random())" / alt="PHP生成圖片驗證碼demo【OOP面向?qū)ο蟀姹尽?quot; >
            <span>看不清?點擊圖片即可切換驗證碼</span><br/>
            <input type="submit" name="sub" value="登錄/注冊" />
        </form>
    </body>
</html>

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

<?php
    /**
     * @Description  網(wǎng)站登錄/注冊驗證碼生成類
     * @Author  趙一鳴
     * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
     * @Date  2016年10月6日 
     */
    class Createcode{
        //畫布資源
        public $img;
        //畫布寬度
        private $img_width;
        //畫布高度
        private $img_height;
        //畫布顏色
        private $img_bgcolor;
        //驗證碼文字內(nèi)容
        private $str_content;
        //生成的驗證碼內(nèi)容
        private $code_content;
        //驗證碼顏色
        private $code_content_color;
        //構(gòu)造函數(shù)
        public function __construct($img_width,$img_height,$str_content,$code_content_color){
            if($this->gdcheck()){
                $this->img_width = $img_width;
                $this->img_height = $img_height;
                $this->str_content = $str_content;
                $this->code_content_color = $code_content_color;
                $this->get_code();
                $this->session_code();
            }
        }
        //生成畫布
        public function get_img(){
            //定義畫布
            $this->img = imagecreatetruecolor($this->img_width, $this->img_height);
            //畫布背景色
            $this->img_bgcolor = imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
            //給畫圖填充背景色
            imagefill($this->img, 0, 0, $this->img_bgcolor);
            //取得畫布的寬高
            $img_width = imagesx($this->img);
            $img_height = imagesy($this->img);
            //畫布中插入驗證碼
            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))));
            //畫布中插入像素點
            $this->get_pix();
            //畫布中插入直線
            $this->get_line();
            //畫布顯示
            header(&#39;Content-type:image/png&#39;);
            imagepng($this->img);
        }
        //生成驗證碼
        private function get_code(){
            $str_content_len = strlen($this->str_content);
            for($i=0;$i<4;$i++){
                $this->code_content .= substr($this->str_content, mt_rand(0,$str_content_len-1),1);
            }
        }
        //生成像素點
        private function get_pix(){
            for($j=0;$j<300;$j++){
                $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)));
            }
            return $image_pix;
        }
        //生成直線
        private function get_line(){
            for($l=0;$l<2;$l++){
                $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)));
            }
            return $img_line;
        }
        //session存儲驗證碼
        private function session_code(){
            session_start();
            $_SESSION[&#39;code&#39;] = $this->code_content;
        }
        //判斷程序是否支持GD庫
        private function gdcheck(){
            if(extension_loaded(&#39;gd&#39;)){
                return true;
            }else{
                return false;
                exit();
            }
        }
    }

3、checkcode.php中的代碼:

<?php
/**
 * @Description  網(wǎng)站登錄/注冊驗證碼生成類
 * @Author  趙一鳴
 * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
 * @Date  2016年10月6日
 */
    header(&#39;Content-type:text/html;charset="utf-8"&#39;);
    session_start();
    if($_POST[&#39;code&#39;]!=&#39;&#39;){
        if($_SESSION[&#39;code&#39;]==$_POST[&#39;code&#39;]){
            echo &#39;<script type="text/javascript">
                    alert("驗證碼填寫成功");
                    history.go(-1);
                </script>&#39;;
        }else{
            echo &#39;<script type="text/javascript">
                    alert("驗證碼填寫失敗");
                    history.go(-1);
                </script>&#39;;
        }
    }

4、showcode.php中的代碼:

<?php
/**
 * @Description  網(wǎng)站登錄/注冊驗證碼生成類
 * @Author  趙一鳴
 * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
 * @Date  2016年10月6日
 */
    function __autoload($classname){
        include strtolower($classname).&#39;.class.php&#39;;
    }
    //定義驗證碼的取值范圍
    $str_content = &#39;abcdefghijklmnopqrstuvwxyz0123456789&#39;;
    //驗證碼文字顏色
    $code_content_color = &#39;#ffffff&#39;;
    //初始化對象
    $code = new Createcode(100,30,$str_content,$code_content_color);
    $code->get_img();

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

轉(zhuǎn)載請注明出處!


本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(quán)歸原作者所有,本站不承擔相應(yīng)法律責任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

熱門話題

Laravel 教程
1600
29
PHP教程
1502
276