验证码PHP类 支持数字,字母,汉字,混合
发布时间:2020-05-25 06:48:37 所属栏目:PHP 来源:互联网
导读:验证码PHP类 支持数字,字母,汉字,混合
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 <?php
// +------------------------------------------------------------------------
// 验证码类,该类的对象能动态获取验证码图片,验证码字符保存在SESSION['code']中
// +------------------------------------------------------------------------
// 支持4种格式 数字 字母 汉字 混合
// +------------------------------------------------------------------------
// @Author: HelloChina([emailprotected])
// +------------------------------------------------------------------------
// @Date: 2012年6月7日11:03:00
// +------------------------------------------------------------------------
// @version 1.0
// +------------------------------------------------------------------------
class Vcode{
protected $width; //验证码宽度
protected $height; //验证码长度
protected $codeNum; //验证码字符个数
protected $codeType; //验证码类型
protected $fontSize; //字符大小
protected $fontType; //字体类型
protected $codeStr; //中文内容
protected $strNum; //中文个数
protected $imageType; //输出图片类型
protected $image; //图片资源
protected $checkCode; //验证码内容
/**
+--------------------------------------------------------------------------------
* 取得验证码信息
+--------------------------------------------------------------------------------
* @param integer $width 验证码宽度
* @param integer $height 验证码高度
* @param integer $codeNum 验证码字符个数
* @param integer $codeType 验证码字符类型 1为数字 2为字母 3为汉字 4为混编
* @param integer $fontSize 验证码字体的大小
* @param string $fontType 验证码字体类型
* @param string $imageType 验证码输出图片类型
* @param string $codestr 中文验证码内容
+--------------------------------------------------------------------------------
*/
public function __construct($width=100,$height=50,$codeNum=4,$codeType=4,$fontSize=12,$fontType='heiti.ttf',$imageType='jpeg',$codeStr='去我饿人他一哦平啊是的飞个好就看了在想才吧你吗'){
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
$this->codeType = $codeType;
$this->fontSize = $fontSize;
$this->fontType = $fontType;
$this->codeStr = $codeStr;
$this->strNum = strlen($this->codeStr)/3-1;
$this->imageType = $imageType;
$this->checkCode = $this->getCheckCode();
}
//+--------------------------------------------------------------------------------
//* 生成验证码字符
//+--------------------------------------------------------------------------------
//* @return string
//+--------------------------------------------------------------------------------
public function __toString(){
$string = implode('',$this->getCheckCode());
$_SESSION["code"]=$string; //加到session中
$this->getImage(); //输出验证码
return '';
}
protected function getCheckCode(){
$string = array();
switch($this->codeType){
case 1:
//数字字符串
$string = array_rand(range(0,9),$this->codeNum);
break;
case 2:
//大字母字符串
$string = array_rand(array_flip(range('A','Z')),$this->codeNum);
break;
case 3:
//汉字字符串
for($i=0; $i<($this->codeNum); $i++){
$start = mt_rand(0,$this->strNum);
$string[$i]= self::msubstr($this->codeStr,$start);
}
break;
case 4:
//混合字符串
for($i=0; $i<($this->codeNum); $i++){
$rand=mt_rand(0,2);
switch($rand){
case 0:
$ascii = mt_rand(48,57);
$string[$i] = sprintf('%c',$ascii);
break;
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
