搜尋
首頁後端開發php教程php圖片驗證碼代碼多個實例

  1. //檔案頭...

  2. header("Content-type: image/png");
  3. //建立真彩色白紙
  4. $im = @imagecreatetruecolor(50, 20) or die("建立圖片失敗");
  5. //取得背景顏色
  6. $background_color = imagecolorallocate($ im, 255, 255, 255);
  7. //填滿背景顏色(這個東西類似油桶)
  8. imagefill($im,0,0,$background_color);
  9. //取得邊框顏色
  10. $border_color = imagecolorallocate($im,200,200,200);
  11. //畫出矩形,邊框顏色200,200,200
  12. imagerectangle($im,0,0,49,19,$border_color);>

    //逐行炫耀背景,全螢幕用1或0

  13. for($i=2;$i //取得隨機淡色
  14. $line_color = imagecolorallocate( $im,rand(200,255),rand(200,255),rand(200,255));
  15. //畫線
  16. imageline($im,2,$i,47,$i,$line_color);
  17. }
  18. //設定字體大小

  19. $font_size=12;
  20. //設定印上去的文字

  21. $Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  22. $Str[1] = "abcdefghijklmnopqrstuvwxyz";
  23. $Str[2] = "01234567891234567890123456" 🎜> $imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)];
  24. $imstr[0]["x"] = rand(2, 5);
  25. $imstr[0]["y"] = rand(1,4);
  26. //取得第2個隨機文字

  27. $imstr[1] ["s"] = $Str[rand(0,2)][rand(0,25)];
  28. $imstr[1]["x"] = $imstr[0]["x"] $ font_size-1 rand(0,1);
  29. $imstr[1]["y"] = rand(1,3);
  30. //取得第3個隨機文字

  31. $imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)];
  32. $imstr[2]["x"] = $imstr[1 ]["x"] $font_size-1 rand(0,1);
  33. $imstr[2]["y"] = rand(1,4);
  34. //取得第4個隨機文字

  35. $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];
  36. $imstr[3]["x "] = $imstr[2]["x"] $font_size-1 rand(0,1);
  37. $imstr[3]["y"] = rand(1,3);
  38. //寫入隨機字串

  39. for($i=0;$i //取得隨機較深顏色
  40. $text_color = imagecolorallocate($ im,rand(50,180),rand(50,180),rand(50,180));
  41. //畫文字
  42. imagechar($im,$font_size,$imstr[$i]["x"],$imstr [$i]["y"],$imstr[$i]["s"],$text_color);
  43. }
  44. //顯示圖片

  45. imagepng($ im);
  46. //銷毀圖片
  47. imagedestroy($im);
  48. ?>
  49. 複製程式碼
複製程式碼

例2,漂亮的PHP圖片驗證碼實例 完整程式碼:

  1. /*
  2. * @Author fy
  3. */
  4. $imgwidth =100; //圖片寬度
  5. */
  6. $imgwidth =100; //圖片寬度
  7. 40; //圖片高度
  8. $codelen =4; //驗證碼長度
  9. $fontsize =20; //字體大小
  10. $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';$im=imagecreatetruecolor($imgwidth,$imgheight);
  11. $while=imageColorAllocate($im,255,255,255);
  12. imagefill($im,00,000 //填滿圖片
  13. //取得字串
  14. $authstr='';
  15. $_len = strlen($charset)-1;
  16. for ($i=0;$i $authstr .= $charset[mt_rand(0,$_len)];
  17. }
  18. session_start();
  19. $_SESSION['scode']=strtolower($authstr );//全部轉為小寫,主要是為了不區分大小寫
  20. //隨機畫點,已經改為劃星星了
  21. for ($i=0;$i $randcolor=imageColorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  22. imagestring($im,mt_rand(1,5), mtm(> imagestring($im,mt_rand(1,5), mtgid(0,$imgid0,$img) ,mt_rand(0,$imgheight), '*',$randcolor);
  23. //imagesetpixel($im,mt_rand(0,$imgwidth),mt_rand(0,$imgheight),$randcolor);
  24. }
  25. //隨機畫線,線條數量=字元數量(隨便)
  26. for($i=0;$i{
  27. $randcolor=imagecolorallocate($ im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  28. imageline($im,0,mt_rand(0,$imgheight),$imgwidth,mt_rand(0,$imgheight),$rand) ;
  29. }
  30. $_x=intval($imgwidth/$codelen); //計算字元距離
  31. $_y=intval($imgheight*0.7); //字元顯示在圖片70%的位置
  32. for($i=0;$i $randcolor=imagecolorallocate($im,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
  33. //imagestring($im,5,$j,5,$imgstr[$i],$color3);
  34. // imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
  35. imagettftext($im,$fontsize,mt_rand(-30,30),$i*$_x 3,$_y,$randcolor,$font ,$authstr[$i]);
  36. }
  37. //產生圖片
  38. header("content-type:image/PNG");
  39. imagePNG($im);
imageDestroy( $im);
複製程式碼

例3,php5 圖片驗證碼實例碼

php5產生圖片驗證碼的範例。

需要用到php GD函式庫函數: 1,imagecreatetruecolor -----創建一個真彩色的圖像 imagecreatetruecolor(int x_size,int y_size) //x表示寬,y表示高 2,imagecolorallocate 為一幅圖像分配顏色(調色板) imagecolorallocate(resource image,int red,int green,int blue)//red,green,blue----三原色 3,imagestring 繪圖函數 iamgestring(resource image,font,int x,int y,內容,顏色); 4,輸出函數 php的header是定義頭的動作,php5中支援3中類型: 1,Content-type:xxxx/yyyy 2,Location:xxxx:yyyy/zzzz 3,Status:nnn xxxxxx xxxx/yyyy表示內容檔案的類型 如:image/gif image/jpeg image/png 範例:header("Content-type:image/jpeg") GD庫中有對應的image類型 imagejpeg(),imagegif(),imagepang() 5,imageline畫線函數 iamgeline(resource image,int x1,int y1,int x2,int y2,int color); image ---圖片 x1 ---啟始座標 y1 x2 ---終點座標 y2 6,imagesetpixel畫點函數 imagesetpixel(resource image,int x,int y,int color) 7,imagettftext帶字體的寫入函數 imagettftext(resource image,float size,float angle,int x,int y,int color,string fontfile,string text) 8,php驗證碼插入中文的方法 iconv("gb2312","utf-8","字串"); //首先要將文字轉換成utf-8格式 9,隨機函數 1,rand([int min,int max]) //rand(1,4) 產生1-4的數 2, dechex(十進制數) //轉換為十六進位

產生驗證碼的步驟: 產生隨機數 -- 建立圖片 -- 隨機數寫成圖片 --儲存在session中

輸入驗證碼例子 gdchek.php

  1. /*
  2. * 產生圖片驗證碼
  3. * and open the template in the editor.
  4. * /
  5. session_start();
  6. for($i=0;$i$rand.=dechex(rand(1,15)); //產生4位數包含十六進位的隨機數
  7. }
  8. $_SESSION[check_gd]=$rand;
  9. $img=imagecreatetruecolor(100,30); //建立圖片
  10. $bg=imagecolorallocate($ img,0,0,0); //第一次產生的是背景顏色
  11. $fc=imagecolorallocate($img,255,255,255); //產生的字型顏色
  12. //為圖片畫線
  13. for($i=0;$i$te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
  14. imageline($ img,rand(0,15),0,100,30,$te);
  15. }
  16. //給圖片畫點
  17. for($i=0;$i$te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
  18. imagesetpixel($img,rand() 0,rand()0,$te);
  19. }
  20. //首先要將文字轉換成utf-8格式
  21. //$str=iconv("gb2312","utf-8","呵呵呵");
  22. //加入中文的驗證
  23. //smkai.ttf是字體文件,為了在別人的電腦中也能起到字體作用,把文件放到專案的根目錄,可以下載,還有本機C:WINDOWSFonts中有
  24. imagettftext($img,11,10,20,20,$fc,"simkai.ttf","你好你好");
  25. //把字串寫在圖片中
  26. // imagestring($img,rand(1,6),rand(3,70),rand(3,16),$rand,$fc);
  27. //輸出圖片
  28. header("Content-type: image/jpeg");
  29. imagejpeg($img);
  30. ?>
複製程式碼

login.php

  1. /*
  2. *
  3. *
  4. */
  5. session_start();
  6. if ($_POST[sub]){
  7. //判斷驗證碼是否相同
  8. if($_POST[gd_pic]==$_SESSION[check_gd]){
  9. echo "驗證成功!";
  10. }else{
  11. echo "驗證碼錯誤";
  12. }
  13. }
  14. ?>
  15. 使用者名稱:
  16. 密碼:
  17. 驗證碼:php圖片驗證碼代碼多個實例
複製程式碼


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
可以在PHP會話中存儲哪些數據?可以在PHP會話中存儲哪些數據?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,數字,數組和原始物。

您如何開始PHP會話?您如何開始PHP會話?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

什麼是會話再生,如何提高安全性?什麼是會話再生,如何提高安全性?May 02, 2025 am 12:15 AM

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

使用PHP會話時有哪些性能考慮?使用PHP會話時有哪些性能考慮?May 02, 2025 am 12:11 AM

PHP会话对应用性能有显著影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHP會話與Cookie有何不同?PHP會話與Cookie有何不同?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHP如何識別用戶的會話?PHP如何識別用戶的會話?May 01, 2025 am 12:23 AM

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

確保PHP會議的一些最佳實踐是什麼?確保PHP會議的一些最佳實踐是什麼?May 01, 2025 am 12:22 AM

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

PHP會話文件默認存儲在哪裡?PHP會話文件默認存儲在哪裡?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用