2007-11-07 08:50

[PHP] 驗證碼

最近在實做文章回復所需要的功能,這是一個既簡單又好用的認證碼,但是他有個缺點,就是單一用戶同時只能存取一次驗證碼,簡單的說就是一次只能寫一篇回應,當然還是可以用其他方式去加強這個缺點。
  1. <?php 
  2. header("Content-type:image/png"); 
  3. header("Content-Disposition:filename=image_code.png"); 
  4. //定義 header 的文件格式為 png,第二個定義其實沒什麼用 
  5.  
  6. // 開啟 session 
  7. if (!isset($_SESSION)) { session_start(); } 
  8.  
  9. // 設定亂數種子 
  10. mt_srand((double)microtime()*1000000); 
  11.  
  12. // 驗證碼變數 
  13. $verification__session = ''; 
  14.  
  15. // 定義顯示在圖片上的文字,可以再加上大寫字母 
  16. $str = 'abcdefghijkmnpqrstuvwxyz1234567890'; 
  17.  
  18. $l = strlen($str); //取得字串長度 
  19.  
  20. //隨機取出 6 個字 
  21. for($i=0; $i<6; $i++){ 
  22.   $num=rand(0,$l-1); 
  23.   $verification__session.= $str[$num]; 
  24. } 
  25.  
  26. // 將驗證碼記錄在 session 中 
  27. $_SESSION["verification__session"] = $verification__session; 
  28.  
  29.  
  30. // 圖片的寬度與高度 
  31. $imageWidth = 160; $imageHeight = 50; 
  32. // 建立圖片物件 
  33. $im = @imagecreatetruecolor($imageWidth, $imageHeight) 
  34. or die("無法建立圖片!"); 
  35.  
  36.  
  37. //主要色彩設定 
  38. // 圖片底色 
  39. $bgColor = imagecolorallocate($im, 255,239,239); 
  40. // 文字顏色 
  41. $Color = imagecolorallocate($im, 255,0,0); 
  42. // 干擾線條顏色 
  43. $gray1 = imagecolorallocate($im, 200,200,200); 
  44. // 干擾像素顏色 
  45. $gray2 = imagecolorallocate($im, 200,200,200); 
  46.  
  47. //設定圖片底色 
  48. imagefill($im,0,0,$bgColor); 
  49.  
  50. //底色干擾線條 
  51. for($i=0; $i<10; $i++){ 
  52.   imageline($im,rand(0,$imageWidth),rand(0,$imageHeight), 
  53.   rand($imageHeight,$imageWidth),rand(0,$imageHeight),$gray1); 
  54. } 
  55.  
  56. //利用true type字型來產生圖片 
  57. imagettftext($im, 20, 0, 25, 35, $Color, 
  58. "/var/lib/defoma/fontconfig.d/D/DejaVu-Serif-Bold.ttf", 
  59. $verification__session); 
  60. /* 
  61. imagettftext (int im, int size, int angle, 
  62. int x, int y, int col, 
  63. string fontfile, string text) 
  64. im 圖片物件 
  65. size 文字大小 
  66. angle 0度將會由左到右讀取文字,而更高的值表示逆時鐘旋轉 
  67. x y 文字起始座標 
  68. col 顏色物件 
  69. fontfile 字形路徑,為主機實體目錄的絕對路徑, 
  70. 可自行設定想要的字型 
  71. text 寫入的文字字串 
  72. */ 
  73.  
  74. // 干擾像素 
  75. for($i=0;$i<90;$i++){ 
  76.   imagesetpixel($im, rand()%$imageWidth , 
  77.   rand()%$imageHeight , $gray2); 
  78. } 
  79.  
  80. imagepng($im); 
  81. imagedestroy($im); 

輸出結果:



參考文章:php確認碼圖片

7 回應:

uhoo.tw 提到...

Good, Thank you! 推。

uhoo.tw 提到...

Good, THank You

匿名 提到...

你的部落格很棒,加油!

DK 提到...

不好意思 請問一下

我把程式碼放到本機執行後 出現
Parse error: syntax error, unexpected T_STRING in C:\wamp\www\aaa\header.php on line 42

我程式碼40~42行是這樣

40// 建立圖片物件
41 $im= @imagecreatetruecolor ($imageWidth, $imageHeight)
42 ordie("無法建立圖片!");


怎麼會這樣@@?

最近剛接觸PHP不曉得這是甚麼問題0.0

Jax Hu 提到...

是 or die 不是 ordie
你需要好用的工具來幫助你
phpEclipse Vim

DK 提到...

不好意思 我又來了0.0

剛剛那問題我解決了

現在換成出現
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\aaa\header.php:9) in C:\wamp\www\aaa\header.php on line 10

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\aaa\header.php:9) in C:\wamp\www\aaa\header.php on line 11

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\aaa\header.php:9) in C:\wamp\www\aaa\header.php on line 15

我把10 11 15行用//把他關掉

問題沒出現了但是一片空白

後來我把
放到第一行

剩下10 11行問題還沒解決

Jax Hu 提到...

你在 <?php 前面出現不正常的字元
這個錯誤訊息在網路上可以找到很多
簡單的說在 header() 及 session_start() 之前不能有輸出