分享一个php 验证码类及调用示例
本文分享一个不错的php 验证码类,后面附有完整的调用示例,有需要的朋友,参考下吧。...
1,php验证码类
<?php // usage: /* 显示验证码: <img src=http://www.jquerycn.cn/a_21545/"captcha.php?cap=login.png"> 检查验证码: 检查输入的验证码与 $_SESSION['login'] 中保存的值是否相等。 */ error_reporting(E_ALL); session_start(); (!isset($_GET['cap']))?die('Error !'):1; $captcha_array=array('login.png','contact.png','comment.png'); (!in_array($_GET['cap'],$captcha_array))?die('Error !'):1; $captcha_cod=new captcha(basename($_GET['cap'],'.png')) ; //验证码类 class captcha { private $session_name; private $image_width; private $image_height; private $cod_length; private $cod_mode; private $font_path; private $avtage_font_size; private $sec_cod; private $res_image; function __construct($name,$width=200,$height=50,$length=5,$mod=2,$font='arial.ttf',$av_font_size=25) { $this-> session_name = $name ; $this-> image_width = $width ; $this-> image_height = $height ; $this-> cod_length = $length ; $this-> mode = $mod ; $this-> font_path = $font ; $this-> avrage_font_size = $av_font_size ; $this->Gen_Cod(); } function Write_Text($text) { $x_pos=10; for($pos=0;$pos<strlen($text);$pos++) { imagettftext($this->res_image,rand($this->avrage_font_size -2,$this->avrage_font_size +2), rand(-40,+40),$x_pos,rand(35,$this->image_height - $this->avrage_font_size), imagecolorallocate($this->res_image,rand(0,150),rand(0,150),rand(0,150)), $this->font_path,$text[$pos]); $x_pos+=($this->image_width/$this->cod_length); } } function Draw_Line() { // for($pos=0;$pos<$this->image_height;$pos+=8) imageline($this->res_image,0,$pos,$this->image_width,$pos,imagecolorallocate($this->res_image,rand(200,230),rand(200,230),rand(200,230))); // for($pos=0;$pos<$this->image_width;$pos+=8) imageline($this->res_image,$pos,0,$pos,$this->image_height,imagecolorallocate($this->res_image,rand(200,230),rand(200,230),rand(200,230))); } function Gen_Cod() { //generate rand cod : //mode:1 => 0-9 , mode:2 => 0-9 , a-z ($this->mode==1) ? $this->sec_cod=substr((string)rand(1000000000,9999999999),0,$this->cod_length) : $this->sec_cod=substr(md5(rand(1000000000,9999999999)),0,$this->cod_length); //set session : $_SESSION[$this->session_name] = $this->sec_cod ; //creat image : $this->res_image=imagecreatetruecolor( $this->image_width , $this->image_height ); //fill color: imagefilledrectangle($this->res_image,0,0,$this->image_width,$this->image_height,imagecolorallocate($this->res_image,255,255,255)); //write text : $this->Write_Text($this->sec_cod); //draw line : $this->Draw_Line(); //output : imagejpeg($this->res_image); header('content-type:image/jpeg'); //destroy: imagedestroy($this->res_image); } }2,php验证码类的调用示例:
<?php session_start(); if(isset($_POST['captchacod'])){ if($_SESSION['login']==$_POST['captchacod'])echo'<a style="color:green">Your Entered Cod Was Correct</a><br>'; else echo'<a style="color:red">Your Entered Cod Was Incorrect</a><br>'; } ?> <img src=http://www.jquerycn.cn/a_21545/"captcha.php?cap=login.png" > <form action="<?php echo $_SERVER['PHP_SELF']; //safe it later (xss)?>" method="post"> <a>INPUT TEXT :</a><br> <input type="text" name="captchacod"><br> <input type="submit" value="check"><br> </form>
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/shell/12122.shtml
上一篇:dos命令之echo命令介绍
下一篇:perl实例之文件读写操作
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
Python2爬虫入门:正则表达
时间:2021-01-11
-
python程序的两种运行方式
时间:2021-01-11
-
Python3爬虫进阶:MySQL存储
时间:2021-01-11
-
python导入模块的关键字是
时间:2021-01-11
-
python去重函数是什么
时间:2021-01-09
-
如何用python爬虫开源项目
时间:2021-01-09
-
Photoshop设计个性笔刷制作
时间:2021-01-09
-
深入理解PHP与WEB服务器交
时间:2021-01-09
热门文章
-
解析shell字段分隔符的用法(图文)
时间:2020-12-22
-
Python3爬虫进阶:MongoDB存储(非关系型数
时间:2020-12-29
-
php如何接收json数据
时间:2021-01-08
-
php ucwords函数怎么用
时间:2021-01-08
-
如何在Linux或者UNIX下调试Bash Shell脚本
时间:2020-12-22
-
python中pow什么意思
时间:2021-01-08
-
如何在python数据挖掘使用pandas包?
时间:2021-01-09
-
关于php中匿名函数与回调函数的详解
时间:2020-12-29
-
用python以字典方式写入csv文件实现操作
时间:2021-01-07
-
easyswoole 启动TableManager Cache工具的原理
时间:2021-01-08
