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

首頁 > 開發 > PHP > 正文

php實現點擊可刷新驗證碼

2024-05-04 23:40:12
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php如何實現點擊即可刷新驗證碼,代碼很詳細,值得大家學習,感興趣的小伙伴們可以參考一下

驗證碼類文件 CreateImg.class.php

 

 
  1. <?php  
  2.  
  3. class ValidationCode  
  4. {  
  5. private $width,$height,$codenum;  
  6. public $checkcode//產生的驗證碼  
  7. private $checkimage//驗證碼圖片  
  8. private $disturbColor = ''//干擾像素  
  9.  
  10. function __construct($width='80',$height='20',$codenum='4')  
  11. {  
  12. $this->width=$width;  
  13. $this->height=$height;  
  14. $this->codenum=$codenum;  
  15. }  
  16. function outImg()  
  17. {  
  18. //輸出頭  
  19. $this->outFileHeader();  
  20. //產生驗證碼  
  21. $this->createCode();  
  22.  
  23. //產生圖片  
  24. $this->createImage();  
  25. //設置干擾像素  
  26. $this->setDisturbColor();  
  27. //往圖片上寫驗證碼  
  28. $this->writeCheckCodeToImage();  
  29. imagepng($this->checkimage);  
  30. imagedestroy($this->checkimage);  
  31. }  
  32.  
  33. private function outFileHeader()  
  34. {  
  35. header ("Content-type: image/png");  
  36. }  
  37.  
  38. private function createCode()  
  39. {  
  40. $this->checkcode = strtoupper(substr(md5(rand()),0,$this->codenum));  
  41. }  
  42.  
  43. private function createImage()  
  44. {  
  45. $this->checkimage = @imagecreate($this->width,$this->height);  
  46. $back = imagecolorallocate($this->checkimage,255,255,255);  
  47. $border = imagecolorallocate($this->checkimage,0,0,0);  
  48. imagefilledrectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$back); // 白色底  
  49. imagerectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$border); // 黑色邊框  
  50. }  
  51.  
  52. private function setDisturbColor()  
  53. {  
  54. for ($i=0;$i<=200;$i++)  
  55. {  
  56. $this->disturbColor = imagecolorallocate($this->checkimage, rand(0,255), rand(0,255), rand(0,255));  
  57. imagesetpixel($this->checkimage,rand(2,128),rand(2,38),$this->disturbColor);  
  58. }  
  59. }  
  60.  
  61. private function writeCheckCodeToImage()  
  62. {  
  63. for ($i=0;$i<=$this->codenum;$i++)  
  64. {  
  65. $bg_color = imagecolorallocate ($this->checkimage, rand(0,255), rand(0,128), rand(0,255));  
  66. $x = floor($this->width/$this->codenum)*$i;  
  67. $y = rand(0,$this->height-15);  
  68. imagechar ($this->checkimage, rand(5,8), $x$y$this->checkcode[$i], $bg_color);  
  69. }  
  70. }  
  71. function __destruct()  
  72. {  
  73. unset($this->width,$this->height,$this->codenum);  
  74. }  
  75. }  
  76. ?> 

包含文件 imgcode.php

 

 
  1. <?php  
  2. session_start();  
  3. require_once('CreateImg.class.php');  
  4. $image = new ValidationCode('80','20','4'); //圖片長度、寬度、字符個數  
  5. $image->outImg();  
  6. $_SESSION['validationcode'] = $image->checkcode; //存貯驗證碼到 $_SESSION 中  
  7. ?> 

前臺文件 demo.php

 

 
  1. ?php  
  2. session_start();  
  3. $test = $_POST['test'];  
  4. $test = strtoupper(trim($test));  
  5. $submit = $_POST['submit'];  
  6. if(isset($submit)){  
  7. if($test==$_SESSION['validationcode']){  
  8. echo 'true';  
  9. else {  
  10. echo 'false';  
  11. }  
  12. }  
  13. ?>  
  14.  
  15. <html>  
  16. <head>  
  17. <title>Image</title>  
  18. <meta http-equiv="content-type" content="text/html;charset=utf-8">  
  19. <script language="javascript">  
  20. function newgdcode(obj,url) {  
  21. obj.src = url+ '?nowtime=' + new Date().getTime();  
  22. //后面傳遞一個隨機參數,否則在IE7和火狐下,不刷新圖片  
  23. }  
  24. </script>  
  25. <body>  
  26. <img src="imgcode.php" alt="看不清楚,換一張" align="absmiddle" style="cursor: pointer;" onclick="javascript:newgdcode(this,this.src);" />  
  27. <form method="POST" name="form1" action="image.php">  
  28. <input type="text" name="test">  
  29. <br />  
  30. <input type="submit" name="submit" value="提交">  
  31. </form>  
  32. </body>  
  33. </head>  
  34. </html> 

以上就是為大家介紹的php點擊驗證碼即可刷新的全部代碼,希望對大家的學習有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 敦煌市| 夏邑县| 瓦房店市| 如东县| 南康市| 会昌县| 莫力| 三穗县| 昭平县| 永善县| 多伦县| 峨眉山市| 彰化县| 佳木斯市| 澳门| 环江| 慈溪市| 英超| 公安县| 启东市| 嘉祥县| 贞丰县| 青铜峡市| 修武县| 贵溪市| 芷江| 敦煌市| 古田县| 淳安县| 房山区| 黔江区| 宜春市| 台山市| 三江| 平泉县| 二连浩特市| 高台县| 彭山县| 海口市| 利辛县| 天峻县|