php验证码封装

/**
* 生成验证码
* @param int $type 1: 纯数字,2:纯字母,3:数字与字母混合 
* @param int $length
* @return string
*/
function buildRandomString($type=1,$length=4){
if ($type == 1) {
$chars = join ( "", range ( 0, 9 ) );
} elseif ($type == 2) {
$chars = join ( "", array_merge ( range ( "a", "z" ), range ( "A", "Z" ) ) );
} elseif ($type == 3) {
$chars = join ( "", array_merge ( range ( "a", "z" ), range ( "A", "Z" ), range ( 0, 9 ) ) );
}
if ($length > strlen ( $chars )) {
exit ( "字符串长度不够" );
}
$chars = str_shuffle ( $chars );
return substr ( $chars, 0, $length );
}

//通过GD库做验证码

function verifyImage($type=1,$length=4,$pixel=0,$line=0,$sess_name = "verify"){

session_start();
//创建画布
$width = 80;
$height = 28;
$image = imagecreatetruecolor ( $width, $height );
$white = imagecolorallocate ( $image, 255, 255, 255 );
$black = imagecolorallocate ( $image, 0, 0, 0 );
//用填充矩形填充画布
imagefilledrectangle ( $image, 1, 1, $width - 2, $height - 2, $white );
$chars = buildRandomString ( $type, $length );
$_SESSION [$sess_name] = $chars;
//$fontfiles = array ("MSYH.TTF", "MSYHBD.TTF", "SIMLI.TTF", "SIMSUN.TTC", "SIMYOU.TTF", "STZHONGS.TTF" );
$fontfiles = array ("SIMYOU.TTF");
//由于字体文件比较大,就只保留一个字体,如果有需要的同学可以自己添加字体,字体在你的电脑中的fonts文件夹里有,直接运行输入fonts就能看到相应字体
for($i = 0; $i < $length; $i ++) {
$size = mt_rand ( 14, 18 );
$angle = mt_rand ( - 15, 15 );
$x = 5 + $i * $size;
$y = mt_rand ( 20, 26 );
$fontfile = $fontfiles[mt_rand ( 0, count ( $fontfiles ) - 1 )];
putenv(‘GDFONTPATH=‘ . realpath(‘.‘));
$color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) );
$text = substr ( $chars, $i, 1 );
//putenv(‘GDFONTPATH=‘ . realpath(‘.‘));

// Name the font to be used (note the lack of the .ttf extension)
imagettftext ( $image, $size, $angle, $x, $y, $color, $fontfile, $text );
}
if ($pixel) {
for($i = 0; $i < 50; $i ++) {
imagesetpixel ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $black );
}
}
if ($line) {
for($i = 1; $i < $line; $i ++) {
$color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) );
imageline ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $color );
}
}

header ( "content-type:image/gif" );//设置头信息
imagegif ( $image );
imagedestroy ( $image );
}

时间: 2024-08-09 02:19:14

php验证码封装的相关文章

iOS开发短信验证码封装 方便好用

---恢复内容开始--- 1.RootViewControler//  Copyright © 2016年 Chason. All rights reserved.// #import "ViewController.h"#import "MessageCordView.h"//手机屏幕的宽和高#define kScreenWidth [UIScreen mainScreen].bounds.size.width#define KScreenHeight [UISc

笔记3 自定义 验证码封装

<?php /** * [code description] * @param integer $width [验证码宽度] * @param integer $height [验证码高度] * @param integer $num [验证码个数] * @param integer $type [验证码类型 1:纯数字 2:字母 3:数字加字母 4:汉字] * @return [type] [description] */ function code($width=160,$height=40

04 第三方之验证码封装

安装模块 1 pip install flask 2 pip install pillow 代码块 1 from flask import Flask,make_response 2 import random 3 import string 4 from io import BytesIO 5 app = Flask(__name__) 6 7 from PIL import Image, ImageDraw, ImageFont 8 9 10 class Captcha: 11 # 生成的验

PHP学习之验证码(2)封装验证码

1. range — 根据范围创建数组,包含指定的元素    2. rand — 产生一个随机整数   3. array_rand — 从数组中取出一个或多个随机的单元,并返回随机条目的一个或多个键. array_rand( array $array[, int $num = 1] ),参数num指明了你想取出多少个单元.   4. array_flip — 交换数组中的键和值 5. mb_substr( string $str, int $start[, int $length = NULL[

读取短信并复制验证码小工具

获取短信有两种方式,第一种是通过广播接收器,第二种通过读取短信数据库. 第一种: 静态注册了一个广播,通过接收包含android.provider.Telephony.SMS_RECEIVED动作的广播,获取新收到的短信内容. <receiver android:name=".GetSMS" android:enabled="true" android:exported="true"> <intent-filter androi

iOS 生成本地验证码

在应用程序注册.登陆或者有关支付确认的界面,经常会用到验证码,验证码有的是通过手机发送获取的,有的是在本地点击获取的,通过手机发送获取的动态验证码可以使用第三方类库实现,本地点击获取的是在本地自己绘制的,下面我们来详细讲解一下本地生成动态验证码的过程. 导入CoreGraphics.framework 验证码封装了一个UIView,在使用的时候直接把这个View加到界面上使用 CaptchaView.h #import <UIKit/UIKit.h> @interface CaptchaVie

[oldboy-django][2深入django]登录注册页面验证码

后台生成随机验证码 #验证码生成 - 在login.html 中有一张验证码图片 设置图片的src url, 相当于发送一次get请求,返回的内容为图片渲染的内容 <div class="col-sm-5"> <input type="password" class="form-control" id="inputPassword3" placeholder="验证码"> </

python web框架Flask——图形验证码及验证码的动态刷新

下列代码都是以自己的项目实例讲述的,相关的文本内容很少,主要说明全在代码注释中 自制图形验证码 这里所说的图形验证码都是自制的图形,通过画布.画笔.画笔字体的颜色绘制而成的.将验证码封装成一个类比较好管理,代码里有绝对详细的注释,当然大家可以直接复制. 里面涉及的字体都是从系统电脑上自带的,大家直接复制当前目录下就可以了. 主目录/utils/captcha/__init__.py import random import string # Image:一个画布 # ImageDraw:一个画笔

estore电子商城-知识整合

Estore电子商城 --- 知识的整合 分析需要的功能: 01.用户注册(发送激活邮件/JS前台实现数据校验/验证码) 02.用户激活 03.用户登录(记住用户名/30天内自动登陆) 04.用户注销 05.添加商品(文件上传) 06.查看商品列表 07.查看商品详情 08.加入购物车(Cookie *session 数据库) 09.增删改查购物车 10.生成订单(多表设计) 11.订单查询(多表查询) 12.订单删除(事务管理/注解+本地线程+动态代理实现事务管理,AOP--面向切面编程) 1