‘壹’ php的验证码提示怎样制作
一般制作验证码会按照下面的几步走:
一:创建出来一个图片,通常我们成为源,可以用imagecreatetruecolor()这个函数搞定
二:给这个源 添加背景色,同时设置文本显示的颜色,GD库函数为我们提供了imagecolorallocate()函数
三:材料弄好了,我们要给它添点内容了,就是我们随机生成的数字或者字母,甚至可以是它们的组合,这里我们可以选择两个函数 imagettftext()、imagesrting(),这两个函数的不同,我们会在后面讲解。
例:
<?php
session_start();//开启session,用来记录获得的验证码,这个函数要写在程序的开头,不然会出现错误
header(“Content-type :image/gif”);//把文件的返回类型设为image/gif格式,这个格式可以输出图片
$codelen=4;//设置你要让用户输入字符的个数,一般为4,过长用户体验不好。
$charset =”ABCDEFGHKLMNPRSTUVWYZ23456789″;//我们可以尽量把一些难以辨认的字符去掉,比如阿拉伯数字0和字母o,这也是提高用户体验的一种方法。
$code =”;
for($i=0;$i<$codelen;$i++){//用for循环得到4个随机的字符,在这里用到了mt_rand,这个函数比rand的效率要高的多,建议大家用这个
$code .=$charset{mt_rand(0,strlen($charset)-1)};
}
$_SESSION['code']=$code;//下篇关于session验证的文章将会用到
$width = 80;
$height = 40;
$im = imagecreatetruecolor($width,$height);//用imagecreatetruecolor()函数来建立一个新的图片,里面的两个数值分别是宽度和高度,这是制作验证码的第一步
$bg = imagecolorallocate($im,255,255,0); //图片背景的颜色,这里是第二步
$textcolor = imagecolorallocate($im,255,0,0);//文字的颜色
imagefill($im,0,0,$bg);//给图片填充背景色
//好了上面的铺垫任务做的差不多了,现在关键就是让字符显示在图片上,这里有两种方法我们一一介绍。
$font =”ggbi.ttf”;//如果你有字体的话 就填上字体的相对路径,如果没有就留空。下面的两个用法我会一一讲解。
if($font!==”"){
for($num=0;$num<4;$num++){
imagettftext($im,mt_rand(12,16),(mt_rand(0,75)+330)%360,5+15*$num,20+mt_rand(2,5),$textcolor,$font,$code[$num]);//这里是第三步
}
}
else{
for($num=0;$num<4;$num++){
imagestring($im,5,10+15*$num,10+mt_rand(0,5),$code[$num],$textcolor);
}
}
header(“Content-type: image/jpeg”);
imagejpeg($im);
?>
‘贰’ 怎么用PHP写个验证码
首先,当用户打开页面时随机产生一个session,然后根据这个值生成验证码图片。
第二,将验证码图片显示到表单上。
第三,当用户提交时表单时,比较session里的值与表单中验证码的值进行比较。
简单的实现过程:http://www.nowamagic.net/php/php_CheckCode.php
复杂的验证码图片生成:http://www.admin5.com/article/20080314/75984.shtml
‘叁’ 怎样制作PHP验证码
<?php
//验证码:文本类型为图像
header("content-type:image/png");
define('TYPE',3);//1.字母 2.字母数字 3.数字 4.逻辑 5.汉字
session_start();
//创建画布
$img = imagecreatetruecolor(90,33);
//创建颜色
//$bgcolor = imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$bgcolor = imagecolorallocate($img,255,255,255);
$textcolor = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
//填充颜色到画布
imagefill($img,0,0,$bgcolor);
//创建但像素的点为干扰项
//for($i=0;$i<100;$i++){
// $pixelcolor = imagecolorallocate($img,rand(150,200),rand(150,200),rand(150,200));
// imagesetpixel($img,rand(0,70),rand(0,30),$pixelcolor);
//}
//
////划线
//$linecolor = imagecolorallocate($img,255,0,0);
//imageline($img,0,0,70,30,$linecolor);
//
////多边形
// $col_poly = imagecolorallocate ( $img , 0 , 255 , 0 );
// imagepolygon ( $img ,
// array (
// 5 , 5,
// 5, 15 ,
// 20,15,
// 20,5
// ),
// 4 ,
// $col_poly );
////弧线
//$arcColor = imagecolorallocate ( $img , 0 , 0 , 255 );
//imagearc($img,35,15,30,30,0,360,$arcColor);
//创建验证码的内容
//#字母
$letter = range('A','Z');
$letterStr = $letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)];
//数字字母
$num = range(0,9);
$numberAndLetter = array_merge($letter,$num);
$nal = $numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)];
//#数字
$number = rand(1000,9999);
//#逻辑
$x = rand(1,9);
$y = rand(1,9);
$expression = $x."+".$y."=?";
$sum = $x+$y;
//#汉字
$CH = array('恭喜发财','财源滚滚','财源广进','才高八斗','学富五车','抬头见喜');
$chstr = $CH[rand(0,count($CH)-1)];
switch(TYPE){
case 1 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$letterStr);$_SESSION['code']=$letterStr;break;
case 2 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$nal);$_SESSION['code']=$nal;break;
case 3 : imagettftext($img,14,0,13,23,$textcolor,'MSYH.TTF',$number);$_SESSION['code']=$number;break;
case 4 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$expression);$_SESSION['code']=$sum;break;
case 5 : imagettftext($img,11,0,6,21,$textcolor,'MSYHBD.TTF',$chstr);$_SESSION['code']=$chstr;break;
}
//输入图像到浏览器
imagepng($img);
?>
‘肆’ 怎么样能够让php生成4位数字验证码
PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有: 1、产生一张png的图片; 2、为图片设置背景色; 3、设置字体颜色和样式; 4、产生4位数的随机的验证码; 5、把产生的每...
‘伍’ php怎么实现验证码的
验证码功能机制实现思路
常规的验证码实现:
a、产生一张png的图片
b、为图片设置背景色
c、设置字体颜色和样式
d、产生4位数的随机的验证码
e、把产生的每个字符调整旋转角度和位置画到png图片上
f、加入噪点和干扰线防止注册机器分析原图片来恶意注册
g、输出图片
h、释放图片所占内存
i、将验证码保存到session或是数据库
j、将和输入的验证码进行对比
短信(邮箱)验证码机制:
a、产生4-6位数的随机的验证码
b、把产生的每个字符保存到session或是数据库
c、将验证码发送到用户的手机(邮箱)
d、用户在规定时间内进行输入
e、将验证码从session或是数据库中取出
f、将和输入的验证码进行对比验证
‘陆’ PHP制作注册页面验证码
使用view-source:查看源码会有错误提示,所以导致图片无法正常输出
‘柒’ php验证码怎么实现
1.新建code.php验证码生成文件
在此之前必须打开php的GD库,修改php.ini文件的配置,取消extension=php_gd2.dll前面的分号。代码如下:
<?php
session_start();
//生成验证码图片
Header("Content-type: image/PNG");
$im = imagecreate(44,18);
$back = ImageColorAllocate($im, 245,245,245);
imagefill($im,0,0,$back); //背景
srand((double)microtime()*1000000);
//生成4位数字
for($i=0;$i<4;$i++){
$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));
$authnum=rand(1,9);
$vcodes.=$authnum;
imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
}
for($i=0;$i<100;$i++) //加入干扰象素
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()p , rand()0 , $randcolor);
}
ImagePNG($im);
ImageDestroy($im);
$_SESSION['Checknum'] = $vcodes;
?>
2. 显示验证码图片
在需要显示验证码的页面中加入
<input type="text" name="passcode" >
<img src="code.php">
3.判断并获取验证码的值
验证码是通过第一步骤代码中的$_SESSION['Checknum'] = $vcodes;赋的值,所以验证码的值存在$_SESSION['Checknum']当中。在验证页面,使用以下代码,
...
session_start();//启动会话
$code=$_POST["passcode"];
if( $code == $_SESSION["Checknum"])
{...}即可完成验证码登录。
运行截图:
望采纳,谢谢
‘捌’ 怎样用PHP制作验证码
<?php
//验证码类
classValidateCode{
private$charset='';//随机因子
private$code;//验证码
private$codelen=4;//验证码长度
private$width=90;//宽度
private$height=40;//高度
private$img;//图形资源句柄
private$font;//指定的字体
private$fontsize=20;//指定字体大小
private$fontcolor;//指定字体颜色
//构造方法初始化
publicfunction__construct(){
$this->font=dirname(__FILE__).'/font/elephant.ttf';//注意字体路径要写对,否则显示不了图片
}
//生成随机码
privatefunctioncreateCode(){
$_len=strlen($this->charset)-1;
for($i=0;$i<$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0,$_len)];
}
}
//生成背景
privatefunctioncreateBg(){
$this->img=imagecreatetruecolor($this->width,$this->height);
$color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
//生成文字
privatefunctioncreateFont(){
$_x=$this->width/$this->codelen;
for($i=0;$i<$this->codelen;$i++){
$this->fontcolor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$this->fontcolor,$this->font,$this->code[$i]);
}
}
//生成线条、雪花
privatefunctioncreateLine(){
//线条
for($i=0;$i<6;$i++){
$color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
//雪花
for($i=0;$i<100;$i++){
$color=imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}
//输出
privatefunctionoutPut(){
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
//对外生成
publicfunctiondoimg(){
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//获取验证码
publicfunctiongetCode(){
returnstrtolower($this->code);
}
}
‘玖’ 怎么用原生php做验证码
<?php
class validateCode{
private $charset=''; //随机因子
private $_len;
private $code; //验证码
private $codelen=4; //验证码长度
private $width=130; //画布宽度
private $height=50; //画布长度
private $img; //图形资源句柄
private $font; //制定字体
private $fontsize=26;//字体大小
private $fontcolor; //字体颜色
//初始化构造函数
public function __construct(){
$this->font=dirname(__file__).'/validateFont.ttf';
//字体文件及路径,__file__表示当前PHP文件绝对路径,dirname表示这个PHP文件的绝对路径的上一层
}
//生成随机码
private function createCode(){
$this->_len=strlen($this->charset)-1; //strlen表示计算字符串长度
for($i=1;$i<=$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0,$this->_len)];
}
}
//生成背景
private function createBG(){
$this->img=imagecreatetruecolor($this->width,$this->height);//创建图像
$color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255)); //分配颜色函数(红,绿,蓝)
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color); //矩形区域着色函数(要填充的图像,x1,y1,x2,y2,要填充的颜色),x1,y1,x2,y2表示矩形的对角线
}
//生成文字
private function createFont(){
$_x=$this->width/$this->codelen;
for($i=0;$i<$this->codelen;$i++){
$this->fontcolor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$this->fontcolor,$this->font,$this->code[$i]); //参数 size 为字形的尺寸;angle 为字型的角度,顺时针计算,0 度为水平,也就是三点钟的方向 (由左到右),90 度则为由下到上的文字;x,y 二参数为文字的坐标值 (原点为左上角);参数 col 为字的颜色;fontfile 为字型文件名称,亦可是远端的文件;text 当然就是字符串内容了。
}
}
//生成线条雪花
private function createLine(){
//线条
for($i=0;$i<6;$i++){
$color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
//雪花
for($i=0;$i<40;$i++){
$color=imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}
//输出
private function outPut(){
header("content-type:image/png");
imagepng($this->img);
imagedestroy($this->img);
}
//对外生成
public function doimg(){
$this->createCode();
$this->createBg();
$this->createFont();
$this->createLine();
$this->outPut();
}
//获取验证码
public function getCode(){
return strtolower($this->code);
}
}
?>
字体自己去找,修改下路径就行了
‘拾’ 如何实现php手机短信验证功能
现在网站在建设网站时为了保证用户信息的真实性,往往会选择发短信给用户手机发验证码信息,只有通过验证的用户才可以注册,这样保证了用户的联系信息资料的100%的准确性。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >
<html xmlns>
<head>
<title></title>
<script src="js/jquery-1.4a2.min.js" type="text/javascript"></script>
<script type="text/javascript">
/*-------------------------------------------*/
var InterValObj; //timer变量,控制时间
var count = 60; //间隔函数,1秒执行
var curCount;//当前剩余秒数
var code = ""; //验证码
var codeLength = 6;//验证码长度
function sendMessage() {
curCount = count;
var dealType; //验证方式
tel = $(’#tel’).val();
if(tel!=’’){
//验证手机有效性
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+d{8})$/;
if(!myreg.test($(’#tel’).val()))
{
alert(’请输入有效的手机号码!’);
return false;
}
tel = $(’#tel’).val();
//产生验证码
for (var i = 0; i < codeLength; i++) {
code += parseInt(Math.random() * 9).toString();
}
//设置button效果,开始计时
$("#btnSendCode").attr("disabled", "true");
$("#btnSendCode").val("请在" + curCount + "秒内输入验证码");
InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次
//向后台发送处理数据
$.ajax({
type: "POST", //用POST方式传输
dataType: "text", //数据格式:JSON
url: ’yanzhengma.php’, //目标地址(根据实际地址)
data: "&tel=" + tel + "&code=" + code,
error: function (XMLHttpRequest, textStatus, errorThrown) { },
success: function (msg){ }
});
}else{
alert(’请填写手机号码’);
}
}
//timer处理函数
function SetRemainTime() {
if (curCount == 0) {
window.clearInterval(InterValObj);//停止计时器
$("#btnSendCode").removeAttr("disabled");//启用按钮
$("#btnSendCode").val("重新发送验证码");
code = ""; //清除验证码。如果不清除,过时间后,输入收到的验证码依然有效
}
else {
curCount--;
$("#btnSendCode").val("请在" + curCount + "秒内输入验证码");
}
}
</script>
</head>
<body>
<input name="tel" id=tel type="text" />
<input id="btnSendCode" type="button" value="发送验证码" onclick="sendMessage()" /></p>
</body>
</html>
第三、调用短信服务器短信接口
整理的页面是yanzhengma.php(具体根据服务商提供信息)
<?php //提交短信
$post_data = array();
$post_data[’userid’] =短信服务商提供ID;
$post_data[’account’] = ’短信服务商提供用户名’;
$post_data[’password’] = ’短信服务商提供密码’;
// Session保存路径
$sessSavePath = dirname(__FILE__)."/../data/sessions/";
if(is_writeable($sessSavePath) && is_readable($sessSavePath)){
session_save_path($sessSavePath);
}
session_register(’mobliecode’);
$_SESSION[’mobilecode’] = $_POST["code"];
$content=’短信验证码:’.$_POST["code"].’【短信验证】’;
$post_data[’content’] = mb_convert_encoding($content,’utf-8’, ’gb2312’); //短信内容需要用urlencode编码下
$post_data[’mobile’] = $_POST["tel"];
$post_data[’sendtime’] = ’’; //不定时发送,值为0,定时发送,输入格式YYYYMMDDHHmmss的日期值
$url=’http://IP:8888/sms.aspx?action=send’;
$o=’’;
foreach ($post_data as $k=>$v)
{
$o.="$k=".$v.’&’;
}
$post_data=substr($o,0,-1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要将结果直接返回到变量里,那加上这句。
$result = curl_exec($ch);
?>
第四:提交表单信息时对短信验证码验证
//手机验证码开始
session_start();
$svalitel = $_SESSION[’mobilecode’];
$vdcodetel = empty($vdcodetel) ? ’’ : strtolower(trim($vdcodetel));
if(strtolower($vdcodetel)!=$svalitel || $svalitel==’’)
{
ResetVdValue();
//echo "Pageviews=".$vdcodetel;
ShowMsg("手机验证码错误!", ’-1’);
exit();
}