PHP验证码函数代码
来源:互联网 发布:百度人工智能api 编辑:程序博客网 时间:2024/06/05 07:23
这篇文章主要分享了php中简单的验证码函数实现代码,代码比较短,但效果却不错,喜欢的朋友可以试试
效果图:
<?php
/**
* vCode(m,n,x,y) m个数字 显示大小为n 边宽x 边高y
* micxp
*www.jbxue.com
*/
session_start();
vCode(4, 15); //4个数字,显示大小为15
function vCode($num = 4, $size = 20, $width = 0, $height = 0) {
!$width && $width = $num * $size * 4 / 5 + 5;
!$height && $height = $size + 10;
// 去掉了 0 1 O l 等
$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
$code = '';
for ($i = 0; $i < $num; $i++) {
$code .= $str[mt_rand(0, strlen($str)-1)];
}
// 画图像
$im = imagecreatetruecolor($width, $height);
// 定义要用到的颜色
$back_color = imagecolorallocate($im, 235, 236, 237);
$boer_color = imagecolorallocate($im, 118, 151, 199);
$text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
// 画背景
imagefilledrectangle($im, 0, 0, $width, $height, $back_color);
// 画边框
imagerectangle($im, 0, 0, $width-1, $height-1, $boer_color);
// 画干扰线
for($i = 0;$i < 5;$i++) {
$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(- $width, $width), mt_rand(- $height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
}
// 画干扰点
for($i = 0;$i < 50;$i++) {
$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color);
}
// 画验证码
@imagefttext($im, $size , 0, 5, $size + 3, $text_color, 'c:\\WINDOWS\\Fonts\\simsun.ttc', $code);
$_SESSION["VerifyCode"]=$code;
header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
header("Content-type: image/png;charset=gb2312");
imagepng($im);
imagedestroy($im);
}
?>
复制代码代码如下:
<?php
/**
* vCode(m,n,x,y) m个数字 显示大小为n 边宽x 边高y
* micxp
*www.jbxue.com
*/
session_start();
vCode(4, 15); //4个数字,显示大小为15
function vCode($num = 4, $size = 20, $width = 0, $height = 0) {
!$width && $width = $num * $size * 4 / 5 + 5;
!$height && $height = $size + 10;
// 去掉了 0 1 O l 等
$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
$code = '';
for ($i = 0; $i < $num; $i++) {
$code .= $str[mt_rand(0, strlen($str)-1)];
}
// 画图像
$im = imagecreatetruecolor($width, $height);
// 定义要用到的颜色
$back_color = imagecolorallocate($im, 235, 236, 237);
$boer_color = imagecolorallocate($im, 118, 151, 199);
$text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
// 画背景
imagefilledrectangle($im, 0, 0, $width, $height, $back_color);
// 画边框
imagerectangle($im, 0, 0, $width-1, $height-1, $boer_color);
// 画干扰线
for($i = 0;$i < 5;$i++) {
$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(- $width, $width), mt_rand(- $height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
}
// 画干扰点
for($i = 0;$i < 50;$i++) {
$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color);
}
// 画验证码
@imagefttext($im, $size , 0, 5, $size + 3, $text_color, 'c:\\WINDOWS\\Fonts\\simsun.ttc', $code);
$_SESSION["VerifyCode"]=$code;
header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
header("Content-type: image/png;charset=gb2312");
imagepng($im);
imagedestroy($im);
}
?>
您可能感兴趣的文章:
php验证码简单函数代码(附效果图)
一个php 验证码类
php验证码的三个实例代码
php验证码的封装类
php5验证码类
- PHP验证码函数代码
- php验证码代码实例
- php jquery 验证码代码
- php生成验证码代码
- PHP验证码代码分享
- PHP验证手机号码和归属地 PHP函数代码
- php生成验证码图片代码
- php生成验证码图片代码
- php 验证码步骤和代码
- PHP 验证码的实现代码
- PHP 身份证号码验证代码
- PHP验证码代码_php验证码类
- php验证码实现代码(3种)验证类
- PHP绘图函数生成图片验证码
- PHP-随机产生密码(验证码)函数
- 《PHP中产生验证码函数封装》
- php图形图像函数的运用-验证码
- php电子邮件验证函数
- 汇编语言的简答入门--斐波那契数列(递归)
- day1
- PHP判断今天是星期几的几种写法
- 一个PHP的远程图片抓取函数分享
- 使用PHP下载CSS文件中的图片的代码
- PHP验证码函数代码
- 背包问题(01背包,完全背包,多重背包)
- WARN org.hibernate.util.JDBCExceptionReporter:100 - SQL Error: 1146, SQLState: 42S02
- Android项目实战--手机卫士20--拿到已经安装了的程序以及程序管理主界面
- 34 - Count and Say
- 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- uboot 启动流程 freescale
- android UI 开机动画和LOGO
- C#中null