当前位置:Gxlcms > PHP教程 > 如何建立php验证码类文件以及调用方式详解

如何建立php验证码类文件以及调用方式详解

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

如何建立php验证码类文件以及调用方式详解

  1. //验证码类
  2. class ValidateCode {
  3. private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
  4. private $code;//验证码
  5. private $codelen = 4;//验证码长度
  6. private $width = 130;//宽度
  7. private $height = 50;//高度
  8. private $img;//图形资源句柄
  9. private $font;//指定的字体
  10. private $fontsize = 20;//指定字体大小
  11. private $fontcolor;//指定字体颜色
  12. //构造方法初始化
  13. public function __construct() {
  14. $this->font = dirname(__FILE__).'/font/elephant.ttf';//注意字体路径要写对,否则显示不了图片
  15. }
  16. //生成随机码
  17. private function createCode() {
  18. $_len = strlen($this->charset)-1;
  19. for ($i=0;$i<$this->codelen;$i++) {
  20. $this->code .= $this->charset[mt_rand(0,$_len)];
  21. }
  22. }
  23. //生成背景
  24. private function createBg() {
  25. $this->img = imagecreatetruecolor($this->width, $this->height);
  26. $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
  27. imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
  28. }
  29. //生成文字
  30. private function createFont() {
  31. $_x = $this->width / $this->codelen;
  32. for ($i=0;$i<$this->codelen;$i++) {
  33. $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
  34. imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);
  35. }
  36. }
  37. //生成线条、雪花
  38. private function createLine() {
  39. //线条
  40. for ($i=0;$i<6;$i++) {
  41. $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
  42. imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
  43. }
  44. //雪花
  45. for ($i=0;$i<100;$i++) {
  46. $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  47. imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
  48. }
  49. }
  50. //
输出 private function outPut() { header('Content-type:image/png'); imagepng($this->img); imagedestroy($this->img); } //对外生成 public function doimg() { $this->createBg(); $this->createCode(); $this->createLine(); $this->createFont(); $this->outPut(); } //获取验证码 public function getCode() { return strtolower($this->code); } }

使用方法:
1、先把验证码类保存为一个名为 ValidateCode.class.php 的文件;
2、新建一个名为 captcha.php 的文件进行调用该类;
captcha.php

3、引用到页面中,代码如下:

4、一个完整的验证页面,代码如下:

  1. <?php
  2. session_start();
  3. //在页首先要开启session,
  4. //error_reporting(2047);
  5. session_destroy();
  6. //将session去掉,以每次都能取新的session值;
  7. //用seesion 效果不错,也很方便
  8. ?>
  9. <html>
  10. <head>
  11. <title>session 图片验证实例</title>
  12. <style type="text/css">
  13. #login p{
  14. margin-top: 15px;
  15. line-height: 20px;
  16. font-size: 14px;
  17. font-weight: bold;
  18. }
  19. #login img{
  20. cursor:pointer;
  21. }
  22. form{
  23. margin-left:20px;
  24. }
  25. </style>
  26. </head>
  27. <body>
  1. <form id="login" action="" method="post">
  2. <p>此例为session验证实例</p>
  3. <p>
  4. <span>验证码:</span>
  5. <input type="text" name="validate" value="" size=10>
  6. <img title="点击刷新" src="./captcha.php" align="absbottom" onclick="this.src='captcha.php?'+Math.random();"></img>
  7. </p>
  8. <p>
  9. <input type="submit">
  10. </p>
  11. </form>
  12. <?php
  13. //打印上一个session;
  14. //echo "上一个session:<b>".$_SESSION["authnum_session"]."</b><br>";
  15. $validate="";
  16. if(isset($_POST["validate"])){
  17. $validate=$_POST["validate"];
  18. echo "您刚才输入的是:".$_POST["validate"]."<br>状态:";
  19. if($validate!=$_SESSION["authnum_session"]){
  20. //判断session值与用户输入的验证码是否一致;
  21. echo "<font color=red>输入有误</font>";
  22. }else{
  23. echo "<font color=green>通过验证</font>";
  24. }
  25. }
  26. ?>

以上就是如何建立php验证码类文件以及调用方式详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行