php学习笔记(十五)验证码类的编写和使用
2012-12-08 13:15
363 查看
<?php session_start(); echo @$_POST["code"]."<br>"; echo $_SESSION["code"]."<br>"; if (strtoupper($_POST["code"])==strtoupper($_SESSION["code"])){ echo "ok"; }else{ echo "error"; } ?>
<html> <body> <br> <form action="imagecode.php" method="post"> <input type="text" name="code" onkeyup="if(this.value!=this.value.toUpperCase())this.value=this.value.toUpperCase()"><img alt="验证码" src="validationcode.php" onclick="this.src='validationcode.php?'+Math.random()"><br> <input type="submit" value="sub"> </form> </body> </html>
<?php session_start(); include 'validationcode.class.php'; $code = new ValidationCode(); $code->showImage(); $_SESSION["code"] = $code->getCheckCode(); ?>
<?php class ValidationCode{ private $width; private $height; private $codeNum; private $image; private $disturbColorNum; private $checkCode; function __construct($width=80,$height=20,$codeNum=4){ $this->width = $width; $this->height = $height; $this->codeNum = $codeNum; $this->checkCode = $this->createCode(); //取整数 $this->disturbColorNum = floor($width*$height/15); } /** * 向浏览器中输出验证码 */ function showImage($fontface=""){ //第一步:创建图像背景 $this->getCreateImage(); //第二步:设置干扰元素 $this->setDisturColor(); //第三步:想图像中画出文本 $this->outPutText($fontface); //第四部:输出图像 $this->outPutImage(); } /** * 获取验证码上的字符串 */ function getCheckCode(){ return $this->checkCode; } /** * 创建图像 */ private function getCreateImage(){ //创建图像资源 $this->image = imagecreatetruecolor($this->width, $this->height); //随即背景色 $backColor = imagecolorallocate($this->image, rand(180, 225), rand(180, 225), rand(180, 225)); //填充背景颜色 imagefill($this->image, 0, 0, $backColor); //设置边框颜色 $border = imagecolorallocate($this->image, 0, 0, 0); //画出边框 imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border); } /** * 创建干扰元素 */ private function setDisturColor(){ for ($i = 0; $i < $this->disturbColorNum; $i++) { //设置颜色 $color = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($this->image, rand(1, $this->width-2), rand(1, $this->height-2), $color); } for ($i = 0; $i < 10; $i++) { //设置颜色 $color = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)); imagearc($this->image, rand(-10, $this->width), rand(-10, $this->height), rand(30, 300), rand(30, 200), 50, 40, $color); } } /** * 创建输出文本 */ private function outPutText($fontface=""){ for ($i = 0; $i < $this->codeNum; $i++) { $color = imagecolorallocate($this->image, rand(0, 150), rand(0, 150), rand(0, 150)); if ($fontface==""){ $font = rand(3, 5); $x = floor($this->width/$this->codeNum)*$i+3; $y = rand(0, $this->height-15); imagechar($this->image, $font, $x, $y, $this->checkCode{$i}, $color); }else{ $size = rand(12, 15); $angle = rand(-30, 30); $x = floor(($this->width-8)/$this->codeNum)*$i+8; $y = rand($size, $this->height); imagettftext($this->image, $size, $angle, $x, $y, $color, $fontface, $this->checkCode{$i}); } } } private function createCode(){ $code = "23456789qwertyuipkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM"; $str = ''; for ($i = 0; $i < $this->codeNum; $i++) { $char = $code{rand(0, strlen($code)-1)}; $str .= $char; } return $str; } /** * 创建输出图像 */ private function outPutImage(){ if (imagetypes() & IMG_GIF){ header("Content-Type:image/gif"); imagegif($this->image); }else if (imagetypes() & IMG_JPG){ header("Content-Type:image/jpeg"); imagejpeg($this->image); }else if (imagetypes() & IMG_PNG){ header("Content-Type:image/png"); imagepng($this->image); }else{ die("php不支持图片创建"); } } /** * 析构函数 */ function __destruct(){ imagedestroy($this->$image); } } ?>
源码下载:php教程学习笔记
相关文章推荐
- 编写GO的WEB开发框架 (十五): 目录结构及如何使用ecgo开发项目
- 使用servlet编写生成验证码
- 分享使用DOM和PHP文本模板编写XML
- js编写的treeview使用方法
- CYQ.Data 轻量数据层之路 使用篇四曲 MAction 增删改(十五)
- 编写一个函数实现n^k,使用递归实现
- 安卓开发之使用Mob的短信接收验证码达到忘记密码找回密码功能*(自绘界面)
- React文档(十五)使用propTypes进行类型检查
- 使用PHP实现自动生成验证码
- 使用GDI+识别简单的图形验证码
- Hibernate的使用component的方法把另外一个实体类作为一个类完全传递,使用一个表示(十五)
- ant的安装、使用,build.xml简单编写
- 深入研究socket编程(3)——使用select函数编写客户端和服务器
- 使用HTML+CSS编写一个灵活的Tab页
- MVC中验证码的简单使用
- 使用WPF编写游戏(一)
- [原]使用Microsoft Expression 编写网页非常棒
- cmake使用总结(二)---工程目录下子目录CMakeLists文件编写
- 使用GDI+绘制验证码
- 编写高质量代码改善C#程序的157个建议——建议138:事件和委托变量使用动词或形容词短语命名