PHP学习实例—4(验证码类的编写)
来源:互联网 发布:dw cc 2017 mac 安装 编辑:程序博客网 时间:2024/06/05 08:42
搞了一天了,看了好多人的博客。。还叫了大神帮忙看程序。。结果也总算是弄出来了。。
总结一:使用GD库要先“在php.in配置文件中去掉extension=php_gd2.dll前的封号";
总结二:用session时在php.in把session.save_path的路径改成你目录下有的文件,我的以前默认是session.save_path = "\tmp",(前面的封号也要去掉),后来我就在C盘里新建了tmp这个文件夹,然后吧路径改成了 "c:\tmp",这样session就能用了。
总结三:要学会去问大神。。哈哈。。
主函数:index.php
<html><head><meta charset="utf-8"> <title>验证码的生成</title><style> * { margin: 0px; padding: 0px; } #contains { width: 500px; margin: 20px auto; background: #0C0; text-align: center; } #img1 { width: 100px;height: 30px;}#code { height: 25px;width: 100px;}input:hover { background: yellow;}</style></head><body> <div id="contains"><table><form method="post" action="index.php"> <tr> <td> 请输入验证码: <input type="text" id="code" name="code" value="<?php echo isset($_POST['sub'])?$_POST['code']:""?>"/></td> <td> <img id="img1" src="./code.php" onclick="javascript:this.src='./code.php?tm='+Math.random();"/> </td><td><input type="submit" name="sub" value="提交"/></td> </tr></form> </table><?php //ob_start(); session_start(); // error_reporting(E_ALL & ~E_NOTICE);if (isset($_POST['sub'])) { // echo $_SESSION['code']."验证码"."<br>";//echo $_POST['code'];if (strtoupper($_SESSION['code']) == strtoupper($_POST['code'])) { //都转换成大写echo "<b>输入成功</b>";}else {echo "<b>输入失败</b>";}}?></div></body></html>主要是ValidateCode类的编写,ValidateCode.class.php,这里也是参考别人的博客的http://blog.csdn.net/liruxing1715/article/details/6897286(类里面用的字体也是到这个博客里面去下载)
<?php//验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyABCDEFGHKMNPRSTUVWXY3456789'; //随机因子 private $code; //验证码 private $codelen = 4; //验证码长度 private $width = 130; //宽度 private $height = 50; //高度 private $img; //图形资源句柄 private $font; //指定的字体 private $fontsize = 20; //指定字体大小 private $fontcolor; //指定字体颜色 //构造方法初始化 public function __construct() { $this->font = 'elephant.ttf'; //这里的字体要先下载 } //生成随机码 private function createCode() { $_len = strlen($this->charset)-1; //获取随机码的长度 for ($i=0;$i<$this->codelen;$i++) { $this->code .= $this->charset[mt_rand(0,$_len)]; //生成验证码 } } //生成背景 private function createBg() { $this->img = imagecreatetruecolor($this->width, $this->height); //创建画布 $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));//随机背景 imagefilledrectangle($this->img,0,0,$this->width,$this->height,$color); //生成矩形 } //生成文字 private function createFont() { $_x = $this->width / $this->codelen; //平分矩形的宽度,使每个字符的位置合适 for ($i=0;$i<$this->codelen;$i++) { $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));//设置字体颜色 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]);//写入验证码 } } //生成线条、雪花 private function createLine() { for ($i=0;$i<6;$i++) { $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color); } for ($i=0;$i<100;$i++) { $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color); } } //输出 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); }}code.php
<?php//ob_start();//define('ROOT_PATH', dirname(__FILE__));session_start(); //启动sessionrequire './core/ValidateCode.class.php'; //先把类包含进来,实际路径根据实际情况进行修改。$_vc = new ValidateCode(); //实例化一个对象$_vc->doimg();//要先运行这个函数,code才有保存$_SESSION['code'] = $_vc->getCode();//验证码保存到SESSION中?>
0 0
- PHP学习实例—4(验证码类的编写)
- PHP学习实例—5(日历类的编写)
- php学习笔记(十五)验证码类的编写和使用
- php验证码的编写
- 用PHP编写CAPTCHA(验证码)
- PHP - 验证码实例
- PHP 验证码实例
- PHP实现自己的验证码(实例)
- php雪花背景验证码的实例
- php雪花背景验证码的实例
- 编写php的extension实例
- 一个PHP验证码类与实例
- php验证码代码实例
- PHP验证码登录实例
- 验证码的编写
- 验证码的编写
- 验证码的编写
- php学习笔记(十一)文件上传类的编写
- Lucene同义词分词器简单实现
- fatjar在线安装地址
- C++类型转换
- Nginx+Tomcat配置
- C语言实现定时器
- PHP学习实例—4(验证码类的编写)
- 如何同时启动多个Tomcat服务器
- Oracle 树操作(select…start with…connect by…prior)
- Loadrunner卸载方法
- 随便做一个
- 第十一章、Tiny4412 U-BOOT移植十一 DDR3简单介绍
- 为什么建Android工程会自动建立appcompat工程
- JQuery中$.ajax()方法参数详解
- 阿里大数据比赛sesson2_RF&GBRT(上)