php圖片驗證碼代碼

來源:互聯網
上載者:User
 代碼如下 複製代碼
date_default_timezone_set('asia/shanghai');
 function setcode($len)
 {
  $code = '';
  for ($i=0;$i<$len;$i++)//產生隨機長度
  {
   $code .= chr(drand());
  }
  return $code;
 }
 //產生一個隨機字元
 function drand()
 {
  $rand = mt_rand(0,2);
  $str = '';
  switch ($rand)
  {
   case 0: $str = mt_rand(48,57);break;//數字
   case 1: $str = mt_rand(65,90);break;//大寫字母
   case 2: $str = mt_rand(97,122);break;//小寫字母
  }
  return $str;
 }
 $_session['checkcode'] = $code = setcode(5);

//php圖片驗證碼原代碼,需支援gd2.dll擴充,需修改php.ini後重啟iis or ap

 代碼如下 複製代碼

session_start();
session_register('safecode');
$type = 'gif';
$width= 40;
$height= 16;
header("content-type: image/".$type);
srand((double)microtime()*1000000);
$randval = randstr(4,"");
if($type!='gif' && function_exists('imagecreatetruecolor')){
     $im = @imagecreatetruecolor($width,$height);
}else{
     $im = @imagecreate($width,$height);
}
     $r = array(225,211,255,223);
     $g = array(225,236,237,215);
     $b = array(225,236,166,125);

     $key = rand(0,3);
 
     $backcolor = imagecolorallocate($im,$r[$key],$g[$key],$b[$key]);//背景色(隨機)
     $bordercolor = imagecolorallocate($im, 0, 0, 0);//邊框色
     $pointcolor = imagecolorallocate($im, 255, 170, 255);//點顏色

     @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backcolor);//背景位置
     @imagerectangle($im, 0, 0, $width-1, $height-1, $bordercolor); //邊框位置
     $stringcolor = imagecolorallocate($im, 255,51,153);

     for($i=0;$i<=100;$i++){
           $pointx = rand(2,$width-2);
           $pointy = rand(2,$height-2);
           @imagesetpixel($im, $pointx, $pointy, $pointcolor);
     }

     @imagestring($im, 3, 5, 1, $randval, $stringcolor);
     $imagefun='image'.$type;
     $imagefun($im);
     @imagedestroy($im);
     $_session['safecode'] = $randval;
//產生隨機字串
function randstr($len=6,$format='all') {
           switch($format) {
                 case 'all':
                 $chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789www.111cn.net'; break;
                 case 'char':
                 $chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; break;
                 case 'number':
                 $chars='0123456789'; break;
                 default :
                 $chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789';
                 break;
           }
     $string="";
     while(strlen($string)<$len)
     $string.=substr($chars,(mt_rand()%strlen($chars)),1);
     return $string;
}

 

//調用此頁面,如果下面的式子成立,則產生驗證碼圖片
if($_get["action"]=="verifycode")
{
    rand_create();
}
//驗證碼圖片產生
function rand_create()
{
    //通知瀏覽器將要輸出png圖片
    header("content-type: image/png");
    //準備好隨機數發生器種子 
    srand((double)microtime()*1000000);
    //準備圖片的相關參數  
    $im = imagecreate(62,20);
    $black = imagecolorallocate($im, 0,0,0);  //rgb黑色標識符
    $white = imagecolorallocate($im, 255,255,255); //rgb白色標識符
    $gray = imagecolorallocate($im, 200,200,200); //rgb灰色標識符
    //開始作圖    
    imagefill($im,0,0,$gray);
    while(($randval=rand()%100000)<10000);{
        $_session["login_check_num"] = $randval;
        //將四位整數驗證碼繪入圖片 
        imagestring($im, 5, 10, 3, $randval, $black);
    }
    //加入幹擾象素   
    for($i=0;$i<200;$i++){
        $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
        imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
    }
    //輸出驗證圖片
    imagepng($im);
    //銷毀映像標識符
    imagedestroy($im);
}
//檢驗驗證碼
function rand_check()
{
    if($_post["reg_rand"] == $_session["login_check_num"]){
        return true;
    }
    else{
        exit("驗證碼輸入錯誤");
    }
}

//驗證碼通過gd產生png圖片,並把$randval隨機數字賦給$_session['login_check_num'],在通過使用者輸入的$_post進行比較,來判斷是否正確。達到需要實現的功能,需要修改php.ini檔案,使php支援gd庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.