前端页面index.php

<?php
header('content-type:text/html;charset=utf-8');
if(isset($_POST['dosubmit'])){session_start();if(strtoupper($_SESSION['code']) == strtoupper($_POST['code'])){echo '输入成功!<br/>';}else{echo '输入不对<br/>';}
}
?>
<form action="index.php" method="post">
用户名:<input type="text" name="username" value=""/><br/>
标题:<input type="text" name="title" value=""/><br/>
内容:<textarea name="content" cols="40" rows="4"></textarea><br/>
验证码:<input type="text" size="4" name="code"><img src="test.php" onclick ="this.src='test.php?'+Math.random()"/><br/>
<input type="submit" name="dosubmit" value="留言"/>
</form>

生成验证码图片test.php

<?php //开启sessionsession_start();require 'print.php';//导入验证码类文件$vcode =new Vcode(80,30,4);//实例化验证码类//将验证码放到服务器自己的空间保存一份$_SESSION['code'] = $vcode->getCode();//将验证码的图片输出$vcode->outimg();//调用方法

验证码类 print.php

<?php class Vcode{private $width;         //宽private $heigth;        //高private $num;           //数量private $code;          //验证码private $img;           //图像资源//构造方法function __construct($width=80,$height=25,$num=4){$this->width        =   $width;$this->heigth       =   $height;$this->num          =   $num;$this->code        =   $this->createCode();}//获取字符的验证码function getCode(){return $this->code;}//输出验证码图形function outimg(){//创建背景 颜色 大小 边框$this->createBack();           //画字 大小 字体颜色$this->outString();//干扰元素 点 线条$this->setDisturb();//输出图像$this->printImg();}//创建背景private function createBack(){//创建资源$this->img = p_w_picpathcreatetruecolor($this->width, $this->heigth);//设置随机背景颜色$bgcolor = p_w_picpathcolorallocate($this->img, rand(225, 255), rand(225, 255), rand(225, 255));//填充背景色p_w_picpathfill($this->img, 0, 0, $bgcolor);//画矩形$bordercolor = p_w_picpathcolorallocate($this->img, 0, 0, 0);p_w_picpathrectangle($this->img, 0, 0, $this->width-1, $this->heigth-1, $bordercolor);}//画字private function  outString(){for($i=0;$i<$this->num;$i++){                        $color  =   p_w_picpathcolorallocate($this->img, rand(0, 128), rand(0, 128), rand(0, 128));$font = rand(3,5);$x = 3 + ($this->width/$this->num)*$i;$y = rand(1, 5);p_w_picpathstring($this->img, $font,$x, $y, $this->code{$i}, $color);}}//设置干扰元素private function setDisturb(){//加上点数for($i=0;$i<100;$i++){$color  =   p_w_picpathcolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));p_w_picpathsetpixel($this->img, rand(1, $this->width-2), rand(1, $this->heigth-2), $color);}//加上线条for($i=0;$i<10;$i++){$color  =   p_w_picpathcolorallocate($this->img, rand(0, 255), rand(0, 128), rand(0, 255));p_w_picpatharc($this->img, rand(-10, $this->width+10), rand(-10, $this->heigth+10), rand(30, 300), rand(30, 300), 55, 44, $color);}}//输出图像private function printImg(){//      header("Content-Type:p_w_picpath/jpeg");//     p_w_picpathjpeg($this->img);if(p_w_picpathtypes() & IMG_GIF){header("Content-Type:p_w_picpath/gif");p_w_picpathjpeg($this->img);}elseif(p_w_picpathtypes() & IMG_JPEG){header("Content-Type:p_w_picpath/jpeg");p_w_picpathjpeg($this->img);}elseif(p_w_picpathtypes() & IMG_JPG){header("Content-Type:p_w_picpath/jpg");p_w_picpathjpeg($this->img);}elseif(p_w_picpathtypes() & IMG_PNG){header("Content-Type:p_w_picpath/png");p_w_picpathjpeg($this->img);}}//生成验证码private function  createCode(){$codes = "23456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";$code = "";for($i=0;$i<$this->num;$i++){$code .=$codes{rand(0,strlen($codes)-1)};}return $code;}//释放图像资源function __destruct(){p_w_picpathdestroy($this->img);}}

转载于:https://blog.51cto.com/11410485/1842156

PHP 验证码   高洛峰 细说PHP相关推荐

  1. php高洛峰_PHP 验证码   高洛峰 细说PHP

    前端页面index.php<?php header('content-type:text/html;charset=utf-8'); if(isset($_POST['dosubmit'])){ ...

  2. php memcache内存大小,PHP memcache 内存缓存 数据库查询 应用 高洛峰 细说PHP

    PHP memcache 内存缓存 数据库查询 应用 高洛峰 细说PHP 发布时间:2020-06-22 18:23:10 来源:51CTO 阅读:232 作者:津沙港湾 栏目:数据库 PHP mem ...

  3. PHP 异常类 Exception 高洛峰 细说PHP

    /** 1.自定义的异常类,必须是系统类Exception的子类* 如果继承Exception类,重写了构造方法,一定要调用一下父类的构造方法.*/class MyException extends ...

  4. PHP 自定义session储存 数据库 方式类   高洛峰 细说PHP

    自定义session储存 数据库 方式类 在php.ini配置文件中更改设置 (Registered_save_handlers 有三种方式 files user memcache) session. ...

  5. PHP 分页类 高洛峰 细说PHP

    分页类 <?php //分页类class Page{private $total; //总记录数private $nums; //每页显示的条数private $pages; //总页数priv ...

  6. PHP 自定义 Smarty 模板引擎类 高洛峰 细说PHP

    smarty模板引擎类简单工作原理 利用Smarty 模板引擎类对模板文件中的变量进行编译,编译过程其实就是利用正则表达式翻译成PHP文件.例如 模板文件中{$title} 利用正则表达式找到并替换成 ...

  7. 高洛峰ajax分页源码,PHP 分页类 高洛峰 细说PHP

    分页类<?php //分页类 class Page{ private $total;          //总记录数 private $nums;        //每页显示的条数 privat ...

  8. php 高洛峰 正则,PHP 自定义 Smarty 模板引擎类 高洛峰 细说PHP

    smarty模板引擎类简单工作原理 利用Smarty 模板引擎类对模板文件中的变量进行编译,编译过程其实就是利用正则表达式翻译成PHP文件.例如 模板文件中{$title}利用正则表达式找到并替换成 ...

  9. php 完成时钟,PHP 绘制时钟 高洛峰 细说PHP

    显示页面代码html > time setInterval(function(){ document.getElementById('time').src="index.php?&qu ...

最新文章

  1. Linux与shell环境,Linux 环境及 Shell 程序
  2. ideal连接数据库报错The server time zone value ‘�й���׼ʱ��’ is unrecognized or represents more than one time
  3. nsa服务器win7系统,Win7系统访问NAS和Samba服务器失败怎么处理
  4. Azure DevOps Server CI - 自搭跨平台容器代理Agents
  5. 了解WWW服务与HTTP协议 【入门与应用】
  6. Hexo+NexT搭建博客笔记
  7. R语言回归表达式中常用的符号
  8. 微课--搭建单机版PySpark开发环境(8分钟)
  9. python中必须使用import引入模块_Python之import方法引入模块详解
  10. GitHub 超 20000 Star,最火开源视频库 FFmpeg 这 20 年!
  11. Fiddler工具工作原理
  12. SWF也能修改!硕思闪客精灵专业Flash反编译工具
  13. Typora安装 Pandoc实现导出功能
  14. Java final String类的详细用法还有特性说明,自己也在学习.
  15. Mac电脑系统设置WIWF热点
  16. liferay6.2 使用默认方式实现可配置的portlet
  17. “程序员”眼中的中秋节
  18. Anbox之构建android.img(三)
  19. StarUML使用文档
  20. 开源规则引擎比较_开源物联网平台ThingsBoard

热门文章

  1. Graphpad Prism 9教程,不会 SPSS,也能搞定卡方检验!
  2. python批量读取dbf_Python 读取DBF/FPT 文件
  3. ZYNQ7000-GPIO EMIO中断实验 程序烧写后自动进一次中断的怪现象
  4. java nextintln_Java对正则表达式的支持(二)
  5. 家用笔记本电脑什么牌子好_南阳家用小型电梯什么牌子好
  6. 英语语法---形容词性从句详解
  7. julia(4)-if ,else
  8. c++计算eigen随笔(10)-数组、矩阵、向量(3)
  9. 【CV】图像分析用 OpenCV 与 Skimage,哪一个更好?
  10. 【机器学习基础】你应该知道的LightGBM各种操作!