国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php驗證碼生成與應用實例

2024-09-04 11:44:28
字體:
來源:轉載
供稿:網友

在寫用戶驗證頁面,如注冊,登錄的時候,為了加強用戶登錄的安全性,添加驗證碼驗證,驗證碼通過gd生成png圖片,并把$randval隨機數字賦給$_session['login_check_num'],在通過用戶輸入的$_post進行比較,來判斷是否正確,達到需要實現的功能,需要修改php.ini文件,使php支持gd庫.

  1. <?php  
  2. //調用此頁面,如果下面的式子成立,則生成驗證碼圖片  
  3. if($_get["action"]=="verifycode")  
  4. {  
  5.     rand_create();  
  6. }  
  7. //驗證碼圖片生成  
  8. function rand_create()  
  9. {  
  10.     //通知瀏覽器將要輸出png圖片  
  11.     header("content-type: image/png");  
  12.     //準備好隨機數發生器種子   
  13.     srand((double)microtime()*1000000);  
  14.     //準備圖片的相關參數    
  15.     $im = imagecreate(62,20);  
  16.     $black = imagecolorallocate($im, 0,0,0);  //rgb黑色標識符  
  17.     $white = imagecolorallocate($im, 255,255,255); //rgb白色標識符  
  18.     $gray = imagecolorallocate($im, 200,200,200); //rgb灰色標識符  
  19.     //開始作圖      
  20.     imagefill($im,0,0,$gray);  
  21.     while(($randval=rand()%100000)<10000);{  
  22.         $_session["login_check_num"] = $randval;  
  23.         //將四位整數驗證碼繪入圖片   
  24.         imagestring($im, 5, 10, 3, $randval$black);  
  25.     }  
  26.     //加入干擾象素     
  27.     for($i=0;$i<200;$i++){  
  28.         $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));  
  29.         imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);  
  30.     }  
  31.     //輸出驗證圖片  
  32.     imagepng($im);  
  33.     //銷毀圖像標識符  
  34.     imagedestroy($im);  
  35. }  
  36. //檢驗驗證碼  
  37. function rand_check()  
  38. //開源代碼Vevb.com 
  39.     if($_post["reg_rand"] == $_session["login_check_num"]){  
  40.         return true;  
  41.     }  
  42.     else{  
  43.         exit("驗證碼輸入錯誤");  
  44.     }  
  45. }  
  46. ?> 

驗證碼,是一種區分用戶是計算機和人的公共全自動程序,在captcha測試中,作為服務器的計算機會自動生成一個問題由用戶來解答,這個問題可以由計算機生成并評判,但是必須只有人類才能解答,由于計算機無法解答captcha的問題,所以回答出問題的用戶就可以被認為是人類.

  1. */ 
  2. session_start(); 
  3. $string = null; 
  4. $im = imagecreatetruecolor(60,25);  //創建真彩圖60*25 
  5. $bg = imagecolorallocate($im,255,255,255);//白色背景 
  6. imagefill($im,0,0,$bg);填充白色 
  7. $x = 5;// 
  8. $y = 0;//文字坐標 
  9. for($i=0 ; $i&lt;4 ;$i++) 
  10. $char = mt_rand(0,9); 
  11. $string .=$char
  12. $y = mt_rand(0,10); 
  13. $ccolor = imagecolorallocate($im,mt_rand(0,230),mt_rand(0,230),mt_rand(0,230)); 
  14. imagechar($im,6,$x,$y,$char,$ccolor);//填充文字 
  15. $x += mt_rand(10,15); 
  16. for($i=0 ;$i 
  17. $x1 = mt_rand(0,80); 
  18. $x2 = mt_rand(0,80); 
  19. $y1 = mt_rand(0,30); 
  20. $y2 = mt_rand(0,30); 
  21. $x2 = $x1 +mt_rand(1,5); 
  22. $y2 = $y1 +mt_rand(1,5); 
  23. $lc = imagecolorallocate($im,mt_rand(0,230),mt_rand(0,230),mt_rand(0,230)); 
  24. imageline($im,$x1,$y1,$x2,$y2,$lc);//填充線條 
  25. $_session['code'] = md5($string); 
  26. header("content-type:image/jpeg"); 
  27. imagepng($im); 
  28. imagedestroy($im); 

當然我們也可以能過同時我們可以明知php ajax來實例驗證功能.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 威远县| 印江| 绥棱县| 和平区| 峨山| 齐齐哈尔市| 大悟县| 昭通市| 全州县| 天柱县| 加查县| 美姑县| 元谋县| 盐池县| 沧源| 华宁县| 石台县| 江西省| 海晏县| 长治县| 虎林市| 兰西县| 乌海市| 苏尼特右旗| 南丹县| 营山县| 平陆县| 留坝县| 奉节县| 枞阳县| 门源| 宜城市| 塔城市| 茌平县| 南木林县| 盐城市| 视频| 家居| 杂多县| 彰化县| 上杭县|