码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2019-05-19 23:14:53      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:doc   ade   封装   head   指定   coding   eset   void   break   

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[, string $encoding = mb_internal_encoding()]] ) 

以下为一个验证码封装实例

vode.php

<?php
function getVerify($fonts=‘fonts/2.ttc‘,$type=1,$length=4,$codeName=‘verifyCode‘,$pixel=50,$line=0,$arc=0,$width=200,$height=50){
//创建一个画布
// $width=200;
// $height=50;
$image=imagecreatetruecolor($width, $height);
//为画布填充颜色
$white=imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, $width, $height, $white);
function getRandColor($image){
return imagecolorallocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
}
// $length=4;
// $type=4;
switch ($type){
case 1:
//数字
$string=join(‘‘,array_rand(range(0,9),$length));
break;
case 2:
//字母
$string=join(‘‘,array_rand(array_flip(array_merge(range(‘a‘,‘z‘),range(‘A‘,‘Z‘))),$length));
break;
case 3:
//数字加字母
$string=join(‘‘,array_rand((array_flip(array_merge(range(0,9),range(‘a‘,‘z‘),range(‘A‘,‘Z‘)))),$length));
break;
case 4:
//汉字
$str=‘看,完,苏,志,燮,今,天,的,热,搜,恋,情,估,计,很,多,迷,妹,又,要,直,呼,心,碎,不,过,也,该,替,偶,像,感,到,开,心,
毕,竟,大,叔,是,真,的,年,纪,不,小,了,同,龄,的,艺,人,很,多,都,抱,娃,了,吧‘;
$arr=explode(‘,‘,$str);
$string=join(‘‘,array_rand(array_flip($arr),$length));
break;
default:
exit(‘非法参数‘);
break;
}
//将验证码存入SESSION
session_start();
$_SESSION[$codeName]=$string;
for ($i=0;$i<$length;$i++){
$size=mt_rand(20,28);
$angle=mt_rand(-15,15);
$x=20+ceil($width/$length)*$i;
$y=mt_rand(ceil($height/3),$height-20);
// $fonts=‘fonts/2.ttc‘;
$text=mb_substr($string,$i,1,‘utf-8‘);
imagettftext($image,$size,$angle,$x,$y,getRandColor($image),$fonts,$text);
}
//添加干扰元素
//添加像素
if($pixel>0){
for($i=0;$i<50;$i++){
imagesetpixel($image,mt_rand(0,200),mt_rand(0,40),getRandColor($image));
}
}
//绘制线段当做干扰元素
if($line>0){
for ($i=0;$i<4;$i++){
imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),getRandColor($image));
}
}
//绘制圆弧
if($arc>0){
for ($i=0;$i<5;$i++){
imagearc($image,mt_rand(0,$width/2),mt_rand(0,$height/2),mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,360),mt_rand(0,360),getRandColor($image));
}
}
//设定向浏览器输出图片类型
header(‘content-type:image/png‘);
//输出图像
imagepng($image);
//销毁资源
imagedestroy($image);
}
?>

reg.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册界面</title>
</head>
<body>
<h1>IMOOC注册界面</h1>
<form action="doAction.php" method="post">
<table bgcolor="#abcdef" width="80%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>用户名</td>
<td><input type="text" name="username" id="" placeholder="请输入用户名..."></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password" id="" placeholder="请输入密码..."></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" name="verify" id="" placeholder="请输入验证码...">
<img src="getVerify.php" id=‘verifyImage‘/><a onclick="document.getElementById(‘verifyImage‘).src=‘getVerify.php?r=‘+Math.random()" href="javascript:void(0)">
看不清,换一个
</a>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="注册"></td>
</tr>
</table>
</form>
</body>
</html>

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

标签:doc   ade   封装   head   指定   coding   eset   void   break   

原文地址:https://www.cnblogs.com/h-s-l/p/10891355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!