Ò»¸ö¼òµ¥°²È«µÄPHPÑéÖ¤ÂëÀà ¸½µ÷Ó÷½·¨

5年以前  |  阅读数:975 次  |  编程语言:PHP 

Ò»¡¢ÑeÖ¤ÂeʾÀý

¶þ¡¢phpÑeÖ¤ÂeÀa£¬secoder.class.php


    <?php
    /**
     * °²È«ÑeÖ¤Âe
     * 
     * °²È«µÄÑeÖ¤ÂeÒª£ºÑeÖ¤ÂeÎÄ×ÖŤÇu¡¢Ðýת£¬Ê¹Óò»Í¬×ÖÌa£¬Ìi¼Ó¸ÉÈÅÂe
     *
     * @author Á÷Ë®ÃÏ´º <cmpan(at)qq.com>
     * @link http://labs.yulans.cn/YL_Security_Secoder
     * @link http://wiki.yulans.cn/docs/yl/security/secoder
     */
    class YL_Security_Secoder {
     /**
     * ÑeÖ¤ÂeµÄsessionµÄϱe
     * 
     * @var string
     */
     //public static $seKey = 'sid.sek ey.ylans.cn';
     public static $seKey = 'sid';
     public static $expire = 3000;  // ÑeÖ¤Âe¹ýÆÚʱ¼a£¨s£(C)
     /**
     * ÑeÖ¤ÂeÖÐʹÓõÄ×Ö*u£¬01IOÈÝÒ×»iÏý£¬½¨Òe²»ÓÃ
     *
     * @var string
     */
     public static $codeSet = '346789ABCDEFGHJKLMNPQRTUVWXY';
     public static $fontSize = 25;  // ÑeÖ¤Âe×ÖÌa´oС(px)
     public static $useCurve = true; // ÊÇ*ñ»­»iÏýÇuÏß
     public static $useNoise = true; // ÊÇ*ñÌi¼ÓÔÓµa 
     public static $imageH = 0;  // ÑeÖ¤ÂeͼƬ¿i
     public static $imageL = 0;  // ÑeÖ¤ÂeͼƬ³¤
     public static $length = 4;  // ÑeÖ¤ÂeλÊý
     public static $bg = array(243, 251, 254); // ±³¾°

     protected static $_image = null;  // ÑeÖ¤ÂeͼƬʵÀý
     protected static $_color = null;  // ÑeÖ¤Âe×ÖÌaÑÕÉ«

     /**
     * Êa³oÑeÖ¤Âe²¢°ÑÑeÖ¤ÂeµÄÖµ±£´aeµÄsessionÖÐ
     * ÑeÖ¤Âe±£´aeµ½sessionµÄ¸ñʽΪ£º $_SESSION[self::$seKey] = array('code' => 'ÑeÖ¤ÂeÖµ', 'time' => 'ÑeÖ¤Âe´´½¨Ê±¼a');
     */
     public static function entry() {
     // ͼƬ¿i(px)
     self::$imageL || self::$imageL = self::$length * self::$fontSize * 1.5 + self::$fontSize*1.5; 
     // ͼƬ¸ß(px)
     self::$imageH || self::$imageH = self::$fontSize * 2;
     // ½¨Á¢Ò»*u self::$imageL x self::$imageH µÄͼÏñ
     self::$_image = imagecreate(self::$imageL, self::$imageH); 
     // ÉeÖñ³¾°  
     imagecolorallocate(self::$_image, self::$bg[0], self::$bg[1], self::$bg[2]); 
     // ÑeÖ¤Âe×ÖÌaËae»uÑÕÉ«
     self::$_color = imagecolorallocate(self::$_image, mt_rand(1,120), mt_rand(1,120), mt_rand(1,120));
     // ÑeÖ¤ÂeʹÓÃËae»u×ÖÌa 
     //$ttf = dirname(__FILE__) . '/ttfs/' . mt_rand(1, 20) . '.ttf'; 4
     $ttf = dirname(__FILE__) . '/ttfs/4.ttf'; 

     if (self::$useNoise) {
     // »aeÔÓµa
     self::_writeNoise();
     } 
     if (self::$useCurve) {
     // »ae¸ÉÈÅÏß
     self::_writeCurve();
     }

     // »aeÑeÖ¤Âe
     $code = array(); // ÑeÖ¤Âe
     $codeNX = 0; // ÑeÖ¤ÂeµÚN¸o×Ö*uµÄ×o±ß¾a
     for ($i = 0; $i<self::$length; $i++) {
     $code[$i] = self::$codeSet[mt_rand(0, 27)];
     $codeNX += mt_rand(self::$fontSize*1.2, self::$fontSize*1.6);
     // дһ¸oÑeÖ¤Âe×Ö*u
     imagettftext(self::$_image, self::$fontSize, mt_rand(-40, 70), $codeNX, self::$fontSize*1.5, self::$_color, $ttf, $code[$i]);
     }

     // ±£´aeÑeÖ¤Âe
     isset($_SESSION) || session_start();
     $_SESSION[self::$seKey]['code'] = join('', $code); // °ÑУÑeÂe±£´aeµ½session
     $_SESSION[self::$seKey]['time'] = time(); // ÑeÖ¤Âe´´½¨Ê±¼a

     header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate');
     header('Cache-Control: post-check=0, pre-check=0', false); 
     header('Pragma: no-cache'); 
     header("content-type: image/png");

     // Êa³oͼÏñ
     imagepng(self::$_image); 
     imagedestroy(self::$_image);
     }

     /** 
     * »­Ò»ÌoÓÉÁ½ÌoÁ¬ÔÚÒ»Æ𹹳ɵÄËae»uÕýÏÒº¯ÊýÇuÏß×÷¸ÉÈÅÏß(Äa¿ÉÒԸijɸu˧µÄÇuÏߺ¯Êý) 
      *  
      *  ¸ßÖеÄÊýѧ¹«Ê½Õ¦¶¼ÍuÁËÄu£¬Ð´³oÀ´
     * ÕýÏÒÐͺ¯Êý½aÎoʽ£ºy=Asin(¦Øx+¦Õ)+b
     *  ¸÷³£ÊýÖµ¶Ôº¯ÊýͼÏñµÄÓ°Ïi£º
     *  A£º¾o¶¨*aÖµ£¨¼´×ÝÏoÀ­ÉiѹËoµÄ±¶Êý£(C)
     *  b£º±iʾ²¨ÐÎÔÚYÖaµÄλÖùØϵ»o×ÝÏoÒƶ¯¾aÀe£¨ÉϼÓϼo£(C)
     *  ¦Õ£º¾o¶¨²¨ÐÎÓeXÖaλÖùØϵ»oºaÏoÒƶ¯¾aÀe£¨×o¼ÓÓÒ¼o£(C)
     *  ¦Ø£º¾o¶¨ÖÜÆÚ£¨×iСÕýÖÜÆÚT=2¦Ð/¨O¦Ø¨O£(C)
     *
     */
     protected static function _writeCurve() {
     $A = mt_rand(1, self::$imageH/2);     // Õñ*u
     $b = mt_rand(-self::$imageH/4, self::$imageH/4); // YÖa*½ÏoÆ«ÒÆÁ¿
     $f = mt_rand(-self::$imageH/4, self::$imageH/4); // XÖa*½ÏoÆ«ÒÆÁ¿
     $T = mt_rand(self::$imageH*1.5, self::$imageL*2); // ÖÜÆÚ
     $w = (2* M_PI)/$T;

     $px1 = 0; // ÇuÏߺa×ø±eÆðʼλÖÃ
     $px2 = mt_rand(self::$imageL/2, self::$imageL * 0.667); // ÇuÏߺa×ø±e½aÊøλÖà  
     for ($px=$px1; $px<=$px2; $px=$px+ 0.9) {
     if ($w!=0) {
     $py = $A * sin($w*$px + $f)+ $b + self::$imageH/2; // y = Asin(¦Øx+¦Õ) + b
     $i = (int) ((self::$fontSize - 6)/4);
     while ($i > 0) { 
      imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // ÕaÀi»­ÏñËصa±ÈimagettftextºÍimagestringÐÔÄÜÒªºÃºÜ¶a  
      $i--;
     }
     }
     }

     $A = mt_rand(1, self::$imageH/2);     // Õñ*u 
     $f = mt_rand(-self::$imageH/4, self::$imageH/4); // XÖa*½ÏoÆ«ÒÆÁ¿
     $T = mt_rand(self::$imageH*1.5, self::$imageL*2); // ÖÜÆÚ
     $w = (2* M_PI)/$T; 
     $b = $py - $A * sin($w*$px + $f) - self::$imageH/2;
     $px1 = $px2;
     $px2 = self::$imageL;
     for ($px=$px1; $px<=$px2; $px=$px+ 0.9) {
     if ($w!=0) {
     $py = $A * sin($w*$px + $f)+ $b + self::$imageH/2; // y = Asin(¦Øx+¦Õ) + b
     $i = (int) ((self::$fontSize - 8)/4);
     while ($i > 0) { 
      imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // ÕaÀi(while)Ñ­»*»­ÏñËصa±ÈimagettftextºÍimagestringÓÃ×ÖÌa´oСһ´Î»­³o£¨²»ÓÃÕawhileÑ­»*£(C)ÐÔÄÜÒªºÃºÜ¶a 
      $i--;
     }
     }
     }
     }

     /**
     * »­ÔÓµa
     * ÍuͼƬÉÏд²»Í¬ÑÕÉ«µÄ×Öĸ»oÊý×Ö
     */
     protected static function _writeNoise() {
     for($i = 0; $i < 10; $i++){
     //ÔÓµaÑÕÉ«
      $noiseColor = imagecolorallocate(
          self::$_image, 
          mt_rand(150,225), 
          mt_rand(150,225), 
          mt_rand(150,225)
         );
     for($j = 0; $j < 5; $j++) {
     // »aeÔÓµa
      imagestring(
       self::$_image,
       5, 
       mt_rand(-10, self::$imageL), 
       mt_rand(-10, self::$imageH), 
       self::$codeSet[mt_rand(0, 27)], // ÔÓµaÎı¾ÎªËae»uµÄ×Öĸ»oÊý×Ö
       $noiseColor
      );
     }
     }
     }

     /**
     * ÑeÖ¤ÑeÖ¤ÂeÊÇ*ñÕýÈ*
     *
     * @param string $code Óû§ÑeÖ¤Âe
     * @param bool Óû§ÑeÖ¤ÂeÊÇ*ñÕýÈ*
     */
     public static function check($code) {
     isset($_SESSION) || session_start();
     // ÑeÖ¤Âe²»ÄÜΪ¿Õ
     if(empty($code) || empty($_SESSION[self::$seKey])) {
     //echo $_SESSION[self::$seKey]['code'].'1';
     return false;

     }
     // session ¹ýÆÚ
     if(time() - $_SESSION[self::$seKey]['time'] > self::$expire) {
     unset($_SESSION[self::$seKey]);
     //echo $_SESSION[self::$seKey]['code'].'2';
     return false;
     //return 0;
     }

    // if($code == $_SESSION[self::$seKey]['code']) {
     if(strtoupper($code) == $_SESSION[self::$seKey]['code']) { //²»Çø*Ö´oСд±È½Ï
     //echo $_SESSION[self::$seKey]['code'].'3';
     return true; 
     }
     //echo $_SESSION[self::$seKey]['code'].'4';
     return false;

     }
    }


    // useage
    /*
    YL_Security_Secoder::$useNoise = false; // Òª¸u°²È«µÄ»°¸Ä³Étrue
    YL_Security_Secoder::$useCurve = true;
    YL_Security_Secoder::entry();
    */

    /*
    // ÑeÖ¤ÑeÖ¤Âe
    if (!YL_Security_Secoder::check(@$_POST['secode'])) {
     print 'error secode';
    }
    */

Èý¡¢µ÷Óý¨

1.ÏÔʾÑeÖ¤ÂeÒ³Ãaecode.php


    <?php 
     session_start();
     require 'secoder.class.php'; //ÏÈ°ÑÀa°uº¬½øÀ´£¬Êµ¼ÊÂ*¾¶¸u¾Ýʵ¼ÊÇe¿o½øÐÐÐ޸ġ£ 
     $vcode = new YL_Security_Secoder();  //ʵÀý»¯Ò»¸o¶ÔÏo 
     $vcode->entry(); 
    ?> 

2.¼i²eÑeÖ¤ÂeÊÇñÕýÈ


    <?php 
     session_start();
     require 'secoder.class.php'; //ÏÈ°ÑÀa°uº¬½øÀ´£¬Êµ¼ÊÂ*¾¶¸u¾Ýʵ¼ÊÇe¿o½øÐÐÐ޸ġ£ 
     $vcode = new YL_Security_Secoder();  //ʵÀý»¯Ò»¸o¶ÔÏo 
     //$vcode->entry(); 
     $code = $_GET['code']; 
     echo $vcode->check($code);  
     //$_SESSION['code'] = $vc->getCode();//ÑeÖ¤Âe±£´aeµ½SESSIONÖÐ
    ?> 

3.ÑeÖ¤ÂeÊaÈe¿oµ÷ÓÃÒ³Ãae


    <img id="messageImg" src='images/tishis2.gif' width='16' height='16'> µ¥»÷ͼƬÖØлñÈ¡ÑeÖ¤Âe<br>
    <a href="#"><img src="code.php" onclick="javascript:this.src='code.php?tm='+Math.random();" />

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例