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

首頁 > 語言 > PHP > 正文

php 簡單的驗證碼生成實例

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

驗證碼可以防止一些用戶重復提交來猜密碼或利用暴力工具來猜密碼,我們加了驗證碼之后給他們增加了不少難度了,下面我提供一款基于session驗證碼程序.

在網站的登陸和注冊的時候,經常會用到驗證碼來防止別人用機械暴力注冊或登陸,加上驗證碼這樣一定程度上讓網站安全很多,下面是一個比較簡單的驗證碼生成,同時給session賦值,代碼如下:

  1. <?php 
  2. session_start(); 
  3. header(“Content-type: image/png”); 
  4. //創建真彩色白紙 
  5. $im = @imagecreatetruecolor(50, 20) or die(“建立圖像失敗”); 
  6. //獲取背景顏色 
  7. $background_color = imagecolorallocate($im, 255, 255, 255); 
  8. //填充背景顏色(這個東西類似油桶) 
  9. imagefill($im,0,0,$background_color); 
  10. //獲取邊框顏色 
  11. $border_color = imagecolorallocate($im,200,200,200); 
  12. //畫矩形,邊框顏色200,200,200 
  13. imagerectangle($im,0,0,49,19,$border_color); 
  14.  
  15. //逐行炫耀背景,全屏用1或0 
  16. for($i=2;$i<18;$i++){ 
  17. //獲取隨機淡色 
  18. $line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255)); 
  19. //畫線 
  20. imageline($im,2,$i,47,$i,$line_color); 
  21.  
  22. //設置字體大小 
  23. $font_size=12; 
  24.  
  25. //設置印上去的文字 
  26. $Str[0] = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”; 
  27. $Str[1] = “abcdefghijklmnopqrstuvwxyz”; 
  28. $Str[2] = “01234567891234567890123456″; 
  29.  
  30. //獲取第1個隨機文字 
  31. $imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)]; 
  32. $imstr[0]["x"] = rand(2,5); 
  33. $imstr[0]["y"] = rand(1,4); 
  34.  
  35. //獲取第2個隨機文字 
  36. $imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)]; 
  37. $imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1); 
  38. $imstr[1]["y"] = rand(1,3); 
  39.  
  40. //獲取第3個隨機文字 
  41. $imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)]; 
  42. $imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1); 
  43. $imstr[2]["y"] = rand(1,4); 
  44.  
  45. //獲取第4個隨機文字 
  46. $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)]; 
  47. $imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1); 
  48. $imstr[3]["y"] = rand(1,3); 
  49.  
  50. //將顯示的數組賦值給session 
  51. $_SESSION['CODE'] = $imstr[0]["s"].$imstr[1]["s"].$imstr[2]["s"].$imstr[3]["s"]; 
  52.  
  53. //寫入隨機字串 
  54. for($i=0;$i<4;$i++){ 
  55. $text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180)); 
  56. imagechar($im,$font_size,$imstr[$i]["x"],$imstr[$i]["y"],$imstr[$i]["s"],$text_color); 
  57. }//開源代碼Vevb.com 
  58. //顯示圖片 
  59. imagepng($im); 
  60. //銷毀圖片 
  61. imagedestroy($im); 
  62. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东台市| 洞头县| 寿光市| 连城县| 唐海县| 瑞安市| 德化县| 苏尼特右旗| 彰化县| 象州县| 亚东县| 灵寿县| 泸溪县| 水城县| 房山区| 灵武市| 承德县| 济源市| 阿鲁科尔沁旗| 河池市| 平利县| 邵阳市| 孝昌县| 芜湖县| 靖边县| 勃利县| 称多县| 探索| 紫金县| 读书| 淮安市| 兴义市| 陆河县| 西平县| 哈尔滨市| 关岭| 元谋县| 台北县| 昌图县| 枣阳市| 长阳|