当前位置:Gxlcms > PHP教程 > 实用的php验证码类分享

实用的php验证码类分享

时间:2021-07-01 10:21:17 帮助过:23人阅读

这篇文章主要为大家详细介绍了一个实用的php验证码类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

万能php验证码类,供大家参考,具体内容如下

code.php是验证码类,类的名称最好和文件名的名称一样,这样有利于我们的查看。

code.php


  1. <?php
  2. header('Content-type:text/html;charset=utf8');
  3. class Code{
  4. // 验证码个数$number
  5. protected $number;
  6. // 验证码类型$codeType
  7. protected $codeType;
  8. // 验证码图像宽度$width
  9. protected $width;
  10. // 验证码$height
  11. protected $height;
  12. // 验证码字符串$code
  13. protected $code;
  14. // 图像资源$image
  15. protected $image;
  16. public function __construct($number=4,$codeType=0,$height=50,$width=100){
  17. //初始化自己的成员属性
  18. $this->number=$number;
  19. $this->codeType=$codeType;
  20. $this->width = $width;
  21. $this->height= $height;
  22. //生成验证码函数
  23. $this->code = $this ->createCode();
  24. }
  25. public function __get($name){
  26. if ($name == 'code'){
  27. return $this->code;
  28. }
  29. return false;
  30. }
  31. /*获取验证码*/
  32. public function getCode() {
  33. return $this->code;
  34. }
  35. /*图像资源销毁*/
  36. public function __destruct(){
  37. imagedestroy($this->image);
  38. }
  39. protected function createCode(){
  40. //通过你的验证码类型生成验证码
  41. switch ($this->codeType){
  42. case 0: //纯数字
  43. $code = $this->getNumberCode();
  44. break;
  45. case 1: //纯字母的
  46. $code = $this->getCharCode();
  47. break;
  48. case 2: //数字和字母混合
  49. $code = $this->getNumCharCode();
  50. break;
  51. default:
  52. die('不支持此类验证码类型');
  53. }
  54. return $code;
  55. }
  56. protected function getNumberCode(){
  57. $str = join('', range(0, 9));
  58. return substr(str_shuffle($str),0, $this->number);
  59. }
  60. protected function getCharCode(){
  61. $str = join('', range('a', 'z'));
  62. $str = $str.strtoupper($str);
  63. return substr(str_shuffle($str),0,$this->number);
  64. }
  65. protected function getNumCharCode(){
  66. $numstr = join('',range(0, 9));
  67. $str =join('', range('a', 'z'));
  68. $str =$numstr.$str.strtoupper($str);
  69. return substr(str_shuffle($str), 0,$this->number);
  70. }
  71. protected function createImage(){
  72. $this->image = imagecreatetruecolor($this->width,
  73. $this->height);
  74. }
  75. protected function fillBack(){
  76. imagefill($this->image, 0, 0, $this->lightColor());
  77. }
  78. /*浅色*/
  79. protected function lightColor(){
  80. return imagecolorallocate($this->image, mt_rand(133,255), mt_rand(133,255), mt_rand(133,255));
  81. }
  82. /*深色*/
  83. protected function darkColor(){
  84. return imagecolorallocate($this->image, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120));
  85. }
  86. protected function drawChar(){
  87. $width = ceil($this->width / $this->number);
  88. for ($i=0; $i< $this->number;$i++){
  89. $x = mt_rand($i*$width+5, ($i+1)*$width-10);
  90. $y = mt_rand(0, $this->height -15);
  91. imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor());
  92. }
  93. }
  94. protected function drawLine(){
  95. for ($i=0;$i<5;$i++) {
  96. imageline($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$this->darkColor());
  97. }
  98. }
  99. protected function drawDisturb(){
  100. for ($i=0;$i<150;$i++){
  101. $x=mt_rand(0, $this->width);
  102. $y=mt_rand(0, $this->height);
  103. imagesetpixel($this->image, $x, $y, $this->lightColor());
  104. }
  105. }
  106. protected function show(){
  107. header('Content-Type:image/png');
  108. imagepng($this->image);
  109. }
  110. public function outImage(){
  111. // 创建画布
  112. $this->createImage();
  113. // 填充背景色
  114. $this->fillBack();
  115. // 将验证码字符串花到画布上
  116. $this->drawChar();
  117. // 添加干扰元素
  118. $this->drawDisturb();
  119. // 添加线条
  120. $this->drawLine();
  121. //
输出并显示 $this->show(); } }

test.php是new一个新的验证码,并把它保存到session中,为我们验证码的验证起到保存和存储的作用。

test.php


  1. <?php
  2. //开启session
  3. session_start();
  4. require_once 'code.php';
  5. $code= new Code(4,1,50,100);
  6. $_SESSION['code']= $code->getCode();
  7. $code->outImage();

login.php就是最后的验证。

login.php


  1. <?php
  2. //开启Session
  3. session_start();
  4. //判断是否提交
  5. if(isset($_POST['dosubmit'])){
  6. //获取session中的验证码并转为小写
  7. $sessionCode=strtolower($_SESSION['code']);
  8. //获取输入的验证码
  9. $code=strtolower($_POST['code']);
  10. //判断是否相等
  11. if($sessionCode==$code){
  12. echo "<script type='text/javascript'>alert('验证码正确!');</script>";
  13. }else{
  14. echo "<script type='text/javascript'>alert('验证码错误!');</script>";
  15. }
  16. }
  17. ?>
  18. <!DOCTYPE html>
  19. <html>
  20. <head>
  21. <title></title>
  22. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
  23. <style type="text/css">
  24. *{margin:0px;padding:0px;}
  25. ul{
  26. width:400px;
  27. list-style:none;
  28. margin:50px auto;
  29. }
  30. li{
  31. padding:12px;
  32. position:relative;
  33. }
  34. label{
  35. width:80px;
  36. display:inline-block;
  37. float:left;
  38. line-height:30px;
  39. }
  40. input[type='text'],input[type='password']{
  41. height:30px;
  42. }
  43. img{
  44. margin-left:10px;
  45. }
  46. input[type="submit"]{
  47. margin-left:80px;
  48. padding:5px 10px;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <form action="login.php" method="post">
  54. <ul>
  55. <li>
  56. <label>用户名:</label>
  57. <input type="text" name="username"/>
  58. </li>
  59. <li>
  60. <label>密码:</label>
  61. <input type="password" name="password"/>
  62. </li>
  63. <li>
  64. <label>验证码:</label>
  65. <input type="text" name="code" size="4" style="float:left"/>
  66. <img src="test.php" onclick="this.src='test.php?Math.random()'"/>
  67. </li>
  68. <li>
  69. <input type="submit" value="登录" name="dosubmit"/>
  70. </li>
  71. </ul>
  72. </form>
  73. </body>
  74. </html>

以上就是实用的php验证码类分享的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行