后盾网视频中出现过的验证码类
这个是我在一个论坛上发现的验证码类,自己改动了一下,效果和后盾网视频中的是类似的,只不过字体会有点不一样,附录里我放了5个自己找的字体,都算是比较容易辨识的。
类的用法请自己研究,这个是很简单的东西。<?php <br />
/**<br>
* 安全验证码<br>
* <br>
* 安全的验证码要:验证码文字扭曲、旋转,使用不同字体,添加干扰码。<br>
* 如果用中文做验证码(我这里不是哦,有兴趣你来改成用中文的),安全度会更好些,但验证码扭曲和旋转是王道,用了字体也算是已经给字体扭曲了,我就不再去给他添一只扭曲的足了。<br>
* 可配置的属性都是一些简单直观的变量,我就不用弄一堆的setter/getter了<br>
*<br>
* @author 流水孟春 <cmpan><br>
* @copyright NEW BSD<br>
* @link http://labs.yulans.cn/YL_Security_Secoder<br>
* @link http://wiki.yulans.cn/docs/yl/security/secoder<br>
*<br>
* @editor <me><br>
* @copyright Apache<br>
* 于2013年12月14日 修改本文件,美化了生成的验证码,并适配于ThinkPHP<br>
*/<br>
class VerifyCode {<br>
/**<br>
* 验证码的session的下标<br>
* <br>
* @var string<br>
*/<br>
public static $seKey = 'vcode';<br>
public static $expire = 100; // 验证码过期时间(s)<br>
/**<br>
* 验证码中使用的字符,01IO容易混淆,建议不用<br>
*<br>
* @var string<br>
*/<br>
public static $codeSet = '346789ABCDEFGHJKLMNPQRTUVWXY';<br>
public static $fontSize = 25; // 验证码字体大小(px)<br>
public static $useCurve = true; // 是否画混淆曲线<br>
public static $useNoise = true; // 是否添加杂点 <br>
public static $imageH = 0; // 验证码图片宽<br>
public static $imageL = 0; // 验证码图片长<br>
public static $length = 4; // 验证码位数<br>
public static $bg = array(243, 251, 254); // 背景<br>
<br>
protected static $_image = null; // 验证码图片实例<br>
protected static $_color = null; // 验证码字体颜色<br>
<br>
public static function createImage() {<br>
// 图片宽(px)<br>
self::$imageL || self::$imageL = self::$length * self::$fontSize * 1.5 + self::$fontSize*1.5; <br>
// 图片高(px)<br>
self::$imageH || self::$imageH = self::$fontSize * 2;<br>
// 建立一幅 self::$imageL x self::$imageH 的图像<br>
self::$_image = imagecreate(self::$imageL, self::$imageH); <br>
// 设置背景 <br>
imagecolorallocate(self::$_image, self::$bg[0], self::$bg[1], self::$bg[2]); <br>
// 验证码字体随机颜色<br>
self::$_color = imagecolorallocate(self::$_image, mt_rand(1,120), mt_rand(1,120), mt_rand(1,120));<br>
// 验证码使用随机字体 <br>
$ttf = APP_PATH . '../Public/font/vcode/'.mt_rand(1, 5).'.ttf'; <br>
<br>
if (self::$useNoise) {<br>
// 绘杂点<br>
self::_writeNoise();<br>
} <br>
if (self::$useCurve) {<br>
// 绘干扰线<br>
self::_writeCurve();<br>
}<br>
<br>
// 绘验证码<br>
$code = array(); // 验证码<br>
$codeNX = 0; // 验证码第N个字符的左边距<br>
for ($i = 0; $i<:></:>
$code[$i] = self::$codeSet[mt_rand(0, 27)];<br>
$codeNX += mt_rand(self::$fontSize*1.2, self::$fontSize*1.6);<br>
// 写一个验证码字符<br>
imagettftext(self::$_image, self::$fontSize, mt_rand(-40, 70), $codeNX, self::$fontSize*1.5, self::$_color, $ttf, $code[$i]);<br>
}<br>
// 保存验证码<br>
XS(self::$seKey,strtolower(join('', $code)),$expire);<br>
<br>
header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate');<br>
header('Cache-Control: post-check=0, pre-check=0', false); <br>
header('Pragma: no-cache'); <br>
header("content-type: image/png");<br>
<br>
// 输出图像<br>
imagepng(self::$_image); <br>
imagedestroy(self::$_image);<br>
}<br>
<br>
/** <br>
* 画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数) <br>
* <br>
* 高中的数学公式咋都忘了涅,写出来<br>
* 正弦型函数解析式:y=Asin(ωx+φ)+b<br>
* 各常数值对函数图像的影响:<br>
* A:决定峰值(即纵向拉伸压缩的倍数)<br>
* b:表示波形在Y轴的位置关系或纵向移动距离(上加下减)<br>
* φ:决定波形与X轴位置关系或横向移动距离(左加右减)<br>
* ω:决定周期(最小正周期T=2π/∣ω∣)<br>
*<br>
*/<br>
protected static function _writeCurve() {<br>
$A = mt_rand(1, self::$imageH/2); // 振幅<br>
$b = mt_rand(-self::$imageH/4, self::$imageH/4); // Y轴方向偏移量<br>
$f = mt_rand(-self::$imageH/4, self::$imageH/4); // X轴方向偏移量<br>
$T = mt_rand(self::$imageH*1.5, self::$imageL*2); // 周期<br>
$w = (2* M_PI)/$T;<br>
<br>
$px1 = 0; // 曲线横坐标起始位置<br>
$px2 = mt_rand(self::$imageL/2, self::$imageL * 0.667); // 曲线横坐标结束位置 <br>
for ($px=$px1; $px
if ($w!=0) {<br>
$py = $A * sin($w*$px + $f)+ $b + self::$imageH/2; // y = Asin(ωx+φ) + b<br>
$i = (int) ((self::$fontSize - 6)/4);<br>
while ($i > 0) { <br>
imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 这里画像素点比imagettftext和imagestring性能要好很多 <br>
$i--;<br>
}<br>
}<br>
}<br>
<br>
$A = mt_rand(1, self::$imageH/2); // 振幅 <br>
$f = mt_rand(-self::$imageH/4, self::$imageH/4); // X轴方向偏移量<br>
$T = mt_rand(self::$imageH*1.5, self::$imageL*2); // 周期<br>
$w = (2* M_PI)/$T; <br>
$b = $py - $A * sin($w*$px + $f) - self::$imageH/2;<br>
$px1 = $px2;<br>
$px2 = self::$imageL;<br>
for ($px=$px1; $px
if ($w!=0) {<br>
$py = $A * sin($w*$px + $f)+ $b + self::$imageH/2; // y = Asin(ωx+φ) + b<br>
$i = (int) ((self::$fontSize - 8)/4);<br>
while ($i > 0) { <br>
imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多 <br>
$i--;<br>
}<br>
}<br>
}<br>
}<br>
<br>
/**<br>
* 画杂点<br>
* 往图片上写不同颜色的字母或数字<br>
*/<br>
protected static function _writeNoise() {<br>
for($i = 0; $i
//杂点颜色<br>
$noiseColor = imagecolorallocate(<br>
self::$_image, <br>
mt_rand(150,225), <br>
mt_rand(150,225), <br>
mt_rand(150,225)<br>
);<br>
for($j = 0; $j
// 绘杂点<br>
imagestring(<br>
self::$_image,<br>
5, <br>
mt_rand(-10, self::$imageL), <br>
mt_rand(-10, self::$imageH), <br>
self::$codeSet[mt_rand(0, 27)], // 杂点文本为随机的字母或数字<br>
$noiseColor<br>
);<br>
}<br>
}<br>
}<br>
<br>
/**<br>
* 验证验证码是否正确<br>
*<br>
* @param string $code 用户验证码<br>
* @return bool 用户验证码是否正确<br>
*/<br>
public static function check($code) {<br>
// 验证码不能为空<br>
if(empty($code)) <br>
return false;<br>
if(session(self::$seKey)==strtolower($code)){<br>
session('vcode',null);<br>
return true;<br>
}else{<br>
return false;<br>
}<br>
}<br>
}</me></cmpan>
vcode.zip
( 376.13 KB 下载:266 次 )
AD:真正免费,域名+虚机+企业邮箱=0元

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

PHP variables store values during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above
