首页 > php教程 > php手册 > 正文

PHP图片验证码

php中文网
发布: 2016-06-06 19:56:57
原创
989人浏览过

php图片验证码原代码,需支持gd2.dll扩展,需修改php.ini后重启iis or apache 调用方法img src=this.php ------------------------------------------------------ 程序代码: ?PHP session_start(); session_register('SafeCode'); $type = 'gif'; $width= 4

php图片验证码原代码,需支持gd2.dll扩展,需修改php.ini后重启iis or apache

调用方法PHP图片验证码

------------------------------------------------------

程序代码:

session_start();
session_register('SafeCode');
$type = 'gif';
$width= 40;
$height= 16;
header("Content-type: image/".$type);
srand((double)microtime()*1000000);
$randval = randStr(4,"");
if($type!='gif' && function_exists('imagecreatetruecolor')){
$im = @imagecreatetruecolor($width,$height);
}else{
$im = @imagecreate($width,$height);
}
$r = Array(225,211,255,223);
$g = Array(225,236,237,215);
$b = Array(225,236,166,125);

$key = rand(0,3);

$backColor = ImageColorAllocate($im,$r[$key],$g[$key],$b[$key]);//背景色(随机)
$borderColor = ImageColorAllocate($im, 0, 0, 0);//边框色
$pointColor = ImageColorAllocate($im, 255, 170, 255);//点颜色

@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);//背景位置
@imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor); //边框位置
$stringColor = ImageColorAllocate($im, 255,51,153);

for($i=0;$i$pointX = rand(2,$width-2);
$pointY = rand(2,$height-2);
@imagesetpixel($im, $pointX, $pointY, $pointColor);
}

@imagestring($im, 3, 5, 1, $randval, $stringColor);
$ImageFun='Image'.$type;
$ImageFun($im);
@ImageDestroy($im);
$_SESSION['SafeCode'] = $randval;
//产生随机字符串
function randStr($len=6,$format='ALL') {
switch($format) {
case 'ALL':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;
case 'CHAR':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;
case 'NUMBER':
$chars='0123456789'; break;
default :
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
break;
}
$string="";
while(strlen($string)$string.=substr($chars,(mt_rand()%strlen($chars)),1);
return $string;
}
?>

立即学习PHP免费学习笔记(深入)”;

-----------------------------

// BY PHP 国度 www.phphot.be
//关闭报错
error_reporting(0);
//图片宽度
$x_size=60;
//图片高度
$y_size=20;
//生成4个随机字符
$nmsg=num_rand(4);
//支持安全通连接吗?
$S=$_SERVER['SERVER_PORT']=='443' ? 1:0;
//使用cookie记录随机码
//expire=0 cookie将会在会话结束后(一般是浏览器关闭)失效。
setCookie('ck_num',md5($nmsg),0,'/','',$S);
//创建宽:60*20的图片(画布)
$aimg = imagecreate($x_size,$y_size);
//设置图片背景色
$back = imagecolorallocate($aimg, 255, 255, 255);
//设置字体颜色
$border = imagecolorallocate($aimg, 0, 0, 0);
//从0,0点填充59*19的白色矩形区域
imagefilledrectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $back);
//从0,0点绘制59*19的黑色矩形边框
imagerectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $border);

for ($i=0;$i     //在图片上写字
     imageString($aimg,5,$i*$x_size/4+3,2, $nmsg[$i],$border);
}
header("Content-type: image/png");
imagepng($aimg);
imagedestroy($aimg);exit;
function num_rand($lenth){
     // 播下一个随机数发生器种子
     //php自4.2.0起,,此参数变为可选项,当该项为空时,会被设为随时数
     mt_srand((double)microtime() * 1000000);
     //产生有4个随机数字的字符串
     for($i=0;$i           $randval.= mt_rand(0,9);
     }
     //对含有4个数字的字符串使用md5加密,长度是32位的
     //从3长度为32的字符中,自最小数起或最大数32-$lenth起,取长度为$lenth的字符串
     $randval=substr(md5($randval),mt_rand(0,32-$lenth),$lenth);
     return $randval;
}
?> 

*********************************************************

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号