3、验证码

  封装的验证码类?

  1. <?php
  2. /*
  3. * 生成验证码
  4. */
  5. class Captcha
  6. {
  7. private $_width = 100;
  8. private $_height = 25;
  9. private $_number = 4; //显示的验证码的字符个数
  10. private $_font = 15; //验证码字体大小
  11. private $_fontfile = 'STXINWEI.TTF';
  12. //创建验证码图像
  13. public function makeImage()
  14. {
  15. # 1. 创建图像资源(画布)
  16. $image = imagecreatetruecolor($this->_width,$this->_height);
  17. //随机填充颜色
  18. //mt_rand(0,255) 生成一个更具有唯一性的随机数 #000 255
  19. $color = imagecolorallocate($image,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
  20. imagefill($image,0,0,$color);
  21. # 2.绘制文字
  22. $code = $this -> makeCode(); //随机生成验证码文字 ab3g
  23. $color = imagecolorallocate($image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
  24. for($i=0;$i<$this->_number;$i++){
  25. imagettftext($image,$this->_font,mt_rand(-30,30),$i*($this->_width/$this->_number)+5,20,$color,$this->_fontfile,$code[$i]);
  26. }
  27. # 3.绘制15条干扰线条
  28. for($i=0;$i<10;$i++){
  29. $color = imagecolorallocate($image,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
  30. imageline($image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color);
  31. }
  32. # 4.设置100个干扰像素点
  33. for($i=0;$i<100;$i++){
  34. imagesetpixel($image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color);
  35. }
  36. # 5.将验证码保存起来吗,便于后面再其他地方使用
  37. //只能使用session来存储,session明天就会讲到
  38. session_start();
  39. $_SESSION['captcha'] = $code;
  40. //在浏览器输出、显示一下
  41. header("Content-Type:image/png");
  42. imagepng($image);
  43. imagedestroy($image);
  44. }
  45. /**
  46. * 随机产生随机数
  47. */
  48. public function makeCode()
  49. {
  50. # 获得字母的范围(大写字母、小写字母)
  51. $lower = range('a','z'); //创建从小a到小z字符范围的数组
  52. $upper = range('A','Z'); //创建从大A到大Z范围的数组
  53. $number = range(3,9); //创建从3到9之间的数字
  54. //将上面的三个数组合并成一个数组
  55. $code = array_merge($lower,$upper,$number);
  56. # 打乱数组元素的顺序
  57. shuffle($code);
  58. //随机从上面的数组中筛选出n个字符,需要通过下标来取数组的元素
  59. $str = '';
  60. for($i=0;$i<$this->_number;$i++){
  61. $str .= $code[$i];
  62. }
  63. return $str;
  64. }
  65. /**
  66. * 验证用户输入的验证码和我们生产的验证码是否一致
  67. * @param [str] $input [输入验证码值]
  68. * @return
  69. */
  70. public function checkCode($input)
  71. {
  72. session_start();
  73. if(strtolower($code) == strtolower($_SESSION['captcha'])){
  74. //说明验证码正确
  75. //echo '验证码正确';
  76. return true;
  77. }else{
  78. //echo '验证码错误';
  79. return false;
  80. }
  81. }
  82. }
  83. ?>

  实例 - 验证码验证(结合上面的验证类)

  html页面?

  1. <form action="captcha.php?act=verify" method="post">
  2. 验证码:<input type="text" name="captcha">
  3. <img src="captcha.php?act=show">
  4. <br>
  5. <input type="submit" value="提交">
  6. </form>

  验证码检测 captcha.php 页面?

  1. //接收地址栏上面的参数
  2. if($_GET['act']=='verify'){
  3. //说明是提交的表单
  4. //接收表单中用户输入的内容
  5. $code = $_POST['captcha'];
  6. //和创建的验证码进行比较
  7. session_start();
  8. //将用户输入的验证码 和 我们创建的统一小写之后再进行比较
  9. if(strtolower($code) == strtolower($_SESSION['captcha'])){
  10. //说明验证码正确
  11. echo '验证码正确';
  12. }else{
  13. echo '验证码错误';
  14. }
  15. }else if($_GET['act']=='show'){
  16. //说明需要显示一个图片
  17. require 'Captcha.class.php';
  18. $captcha = new Captcha();
  19. $captcha -> makeImage();
  20. }