ホームページ バックエンド開発 PHPチュートリアル PHP は検証コードを生成します code_PHP チュートリアル

PHP は検証コードを生成します code_PHP チュートリアル

Jul 13, 2016 am 10:45 AM
class php private コード チュートリアル 生成する 確認する

php教程生成验证码类代码
*/
クラスセキュアコード
{
    プライベート静的 $instance=null;
    プライベート $コード = '';
    プライベート $fontfile;
    プライベート $validate;
    プライベート $image;
    private $specialadd = 'セキュアコードの特別な文字列';
    プライベート $codeexpire=86400;
    private $codecookiename='secure_code';

/**
*施工方法
​​*/
    プライベート関数 securecode()
    {
        $this->fontfile = dirname( __file__ ) 。 '/arial.ttf';
    }

プライベート関数 __construct()
    {
        $this->securecode();
    }
   
    パブリック静的関数 getinstance()
    {
        if (self::$instance==null)
            self::$instance=新しいself();
       
        self::$instance を返します;
    }

/**
* フォントファイルへのパスを指定します。デフォルトは現在のフォルダー内の arial.ttf ファイルです
* @param $fontfile ファイルパス
* @return void
​​*/
    関数loadfont($fontfile)
    {
        $this->fontfile = $fontfile;
    }

/**
* 画像出力メソッド。このメソッドを実行する前に、プログラムはいかなる形式の出力も持たないでください
* @return void;
​​*/
    関数ストローク()
    {
        $this->savecode();
        self::sendheader();
        imagegif( $this->validate );
        imagedestroy( $this->validate );
        imagedestroy( $this->image );
    }

/**
*写真の保存方法
* @param $filename 保存パス
* @return void
​​*/
    関数 save($filename)
    {
        $this->savecode();
        imagegif( $this->validate , $filename );
        imagedestroy( $this->validate );
        imagedestroy( $this->image );
    }
   
  /**
※認証コードの確認方法
* @param $input 検証する文字列、つまりユーザーの入力
* @return ブール値の検証結果
​​*/
    関数 verify($input)
    {
        $input=strto lower($input);
        $targetcode=$this->認証コード($input);
        $code=$this->getcookie();
        if (empty($code)||$code!=$targetcode)
            $result= false;
        それ以外
            $result=true;
        $_cookie[$this->コードクッキー名]='';
        setcookie ( $this->codecookiename, '', - 1 );
        $result を返します;
    }

/**
*画像の作成方法
* @return void;
​​*/
    関数createimage()
    {
        $this->randcode();
       
        $サイズ = 30;
        $width = 90;
        $高さ = 35;
        $degrees = 配列 (
            rand( 0 , 30 )、rand( 0 , 30 )、rand( 0 , 30 )、rand( 0 , 30 )
        );
       

for ($i = 0; $i         {
            if (rand() % 2);
            それ以外の場合 $degrees[$i] = -$degrees[$i];
        }
       
        $this->image = imagecreatetruecolor( $size , $size );
        $this->validate = imagecreatetruecolor( $width , $height );
        $back = imagecolorallocate( $this->image , 255 , 255 , 255 );
        $border = imagecolorallocate( $this->image , 0 , 0 , 0 );
        imagefilledrectangle( $this->validate , 0 , 0 , $width , $height , $back );
       
        for ($i = 0; $i         {
            $temp = self::rgbtohsv( rand( 0 , 250 ) , rand( 0 , 150 ) , rand( 0 , 250 ) );
           
            if ($temp[2] > 60) $temp[2] = 60;
           
            $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
            $textcolor[$i] = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
        }
       
        for ($i = 0; $i         {
            $randpixelcolor = imagecolorallocate( $this->validate , rand( 0 , 255 ) , rand( 0 , 255 ) , rand( 0 , 255 ) );
            imagesetpixel( $this->validate , rand( 1 , 87 ) , rand( 1 , 35 ) , $randpixelcolor );
        }
       
        $temp = self::rgbtohsv( rand( 220 , 255 ) , rand( 220 , 255 ) , rand( 220 , 255 ) );
       
        if ($temp[2]        
        $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
        $randlinecolor = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
       
        self::imagelinethick( $this->validate , $textcolor[rand( 0 , 3 )] );
       
        imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
        putenv( 'gdfontpath=' . realpath( '.' ) );
       
        // 使用するフォントの名前を指定します (拡張子 .ttf がないことに注意してください
)         imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[0] , $this->fontfile , $this->code[0] );
       
        $this->image = imagerotate( $this->image , $degrees[0] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 1 , 4 , 4 , 5 , imagex( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 ) ;
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[1] , $this->fontfile , $this->code[1] );
        $this->image = imagerotate( $this->image , $degrees[1] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 21 , 4 , 4 , 5 , imagex( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 ) ;
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[2] , $this->fontfile , $this->code[2] );
        $this->image = imagerotate( $this->image , $degrees[2] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 41 , 4 , 4 , 5 , imagex( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 ) ;
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[3] , $this->fontfile , $this->code[3] );
        $this->image = imagerotate( $this->image , $degrees[3] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 61 , 4 , 4 , 5 , imagex( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 ) ;
        imagerectangle( $this->validate , 0 , 0 , $width - 1 , $height - 1 , $border );
    }
   
    /**
* ランダムに生成された確認コードを取得します
* @return string ランダムな検証コード。返された検証コードはいかなる方法でも処理されません
​​*/
    関数getcode()
    {
        $this->コードを返す;
    }

/**
*ランダムコードを生成する方法
* @return void;
​​*/
    保護された関数 rancode()
    {
        $alphastr = 'abcdefghijklmnpqrstuvwxyz123456789';
        $randstr = 配列 (
            $alphastr{rand( 0 , 33 )}、$alphastr{rand( 0 , 33 )}、$alphastr{rand( 0 , 33 )}、$alphastr{rand( 0 , 33 )}
        );
        $this->code = strto lower( $randstr[0] . $randstr[1] . $randstr[2] . $randstr[3] );
    }

/**
* * RGBカラーからHSVカラーへの変換方法
* @param $r
* @param $g
* @param $b
* @return 配列 hsv 配列
​​*/
    保護された静的関数 rgbtohsv($r, $g, $b)
    {
        $tmp = min( $r , $g );
        $min = min( $tmp , $b );
        $tmp = max( $r , $g );
        $max = max( $tmp , $b );
        $v = $max;
        $delta = $max - $min;
       
        if ($max != 0) $s = $delta / $max; //
        それ以外
        {
            $s = 0;
            //$h = 未定義色;
            戻ります;
        }
        if ($r == $max) $h = ($g - $b) / $delta; // イエローとマゼンタの間
        else if ($g == $max) $h = 2 + ($b - $r) / $delta; // シアンとイエローの間
        それ以外の場合、$h = 4 + ($r - $g) / $delta; // マゼンタとシアンの間
       


        $h *= 60; // 度
        if ($h         配列を返します (
            $h、$s、$v
        );
    }

/**
* * 前のメソッドと同じですが、関数が逆になります
* @param $h
* @param $s
* @param $v
* @return 配列 rgb 配列
​​*/
    保護された静的関数 hsvtorgb($h, $s, $v)
    {
        if ($s == 0)
        {
            // 無彩色(グレー)
            $r = $g = $b = $v;
            戻ります;
        }
       
        $h /= 60; // セクター0~5
        $i = フロア( $h );
        $f = $h - $i; // h
の階乗部分         $p = $v * (1 - $s);
        $q = $v * (1 - $s * $f);
        $t = $v * (1 - $s * (1 - $f));
       
        スイッチ($i)
        {
            ケース0:
                $r = $v;
                $g = $t;
                $b = $p;
                休憩;
            ケース1:
                $r = $q;
                $g = $v;
                $b = $p;
                休憩;
            ケース2:
                $r = $p;
                $g = $v;
                $b = $t;
                休憩;
            ケース3:
                $r = $p;
                $g = $q;
                $b = $v;
                休憩;
            ケース4:
                $r = $t;
                $g = $p;
                $b = $v;
                休憩;
            デフォルト : // ケース 5:
                $r = $v;
                $g = $p;
                $b = $q;
                休憩;
        }
        配列を返します (
            $r、$g、$b
        );
    }

/**
* Cookie を使用して認証コードを保存する方法
* @return void
​​*/
    保護された関数 savecode()
    {
        $code = $this->authcode($this->code);
        $this->setcookie($code);
    }

/**
* * 認証コードCookie値の取得方法
* @return 文字列クッキー値
​​*/
    保護された関数 getcookie()
    {
        if (empty( $_cookie[$this->codecookiename] ))
        {
            戻ります '';
        }
        それ以外
        {
            return addslashes($_cookie[$this->codecookiename]);
        }
    }
   
    /**
* * 認証コードクッキーの作成方法
* @param string $code 保存する認証コード
* @return void
​​*/
    保護された関数 setcookie($code)
    {
        $expire = $this->コード期限切れ> 0 ? $this->codeexpire + time() : 0;
        setcookie( $this->codecookiename , $code, $expire );
    }
   
    /**
※認証コードの暗号化方式
* @param string $code 暗号化するランダムコード
* @return 混合実行結果
​​*/
    保護された関数の認証コード($code)
    {
        return md5($code.$this->specialadd);
    }
   
    /**
※干渉線の生成方法
* @param resource $image 画像リソースハンドル
* @param string $color 干渉線の色
​​*/
    保護された静的関数 imagelinethick($image, $color)
    {
        $k = ランド( 5 , 20 );
        for ($px = 0; $px         {
            $y = $k * sin( 0.1 * ($px) ); //$y=200+10*sin(0.1*($px-200));
            for ($i = 0; $i             {
                imagesetpixel( $image , $px , $y + 10 + $i , $color );
            }
       
        }
    }

/**
※httpヘッダーの設定方法
* @return void
​​*/
    保護された静的関数 sendheader()
    {
        header( "プラグマ: キャッシュなし" );
        header( "キャッシュ制御: max-age=1、s-maxage=1、キャッシュなし、必須再検証" );
        header( 'content-type: image/gif' );
    }
}
http://down.bKjia.c0m/down/code/php/qitayuanma/2010/1220/22330.html

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/632991.html技術記事 PHP チュートリアルは検証コードを生成します */ class securecode { private static $instance=null = '';
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Dec 24, 2024 pm 04:42 PM

PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

CakePHP の日付と時刻 CakePHP の日付と時刻 Sep 10, 2024 pm 05:27 PM

Cakephp4 で日付と時刻を操作するには、利用可能な FrozenTime クラスを利用します。

CakePHP ファイルのアップロード CakePHP ファイルのアップロード Sep 10, 2024 pm 05:27 PM

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP について話し合う CakePHP について話し合う Sep 10, 2024 pm 05:28 PM

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

CakePHP バリデータの作成 CakePHP バリデータの作成 Sep 10, 2024 pm 05:26 PM

Validator は、コントローラーに次の 2 行を追加することで作成できます。

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 Dec 20, 2024 am 11:31 AM

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、

CakePHP クイックガイド CakePHP クイックガイド Sep 10, 2024 pm 05:27 PM

CakePHP はオープンソースの MVC フレームワークです。これにより、アプリケーションの開発、展開、保守がはるかに簡単になります。 CakePHP には、最も一般的なタスクの過負荷を軽減するためのライブラリが多数あります。

PHPでHTML/XMLを解析および処理するにはどうすればよいですか? PHPでHTML/XMLを解析および処理するにはどうすればよいですか? Feb 07, 2025 am 11:57 AM

このチュートリアルでは、PHPを使用してXMLドキュメントを効率的に処理する方法を示しています。 XML(拡張可能なマークアップ言語)は、人間の読みやすさとマシン解析の両方に合わせて設計された多用途のテキストベースのマークアップ言語です。一般的にデータストレージに使用されます

See all articles