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

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" alt="" id='verifyImage'/><a οnclick="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>

转载于:https://www.cnblogs.com/h-s-l/p/10891355.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的原生PHP验证码类的示例: ```php class Captcha { // 验证码字符长度 protected $length = 4; // 验证码宽度 protected $width = 100; // 验证码高度 protected $height = 40; // 验证码字符集 protected $charset = '0123456789'; // 验证码图片 protected $image; // 构造函数 public function __construct() { $this->create(); } // 生成验证码 public function create() { $this->image = imagecreatetruecolor($this->width, $this->height); $bgColor = imagecolorallocate($this->image, 255, 255, 255); imagefill($this->image, 0, 0, $bgColor); $code = $this->generateCode(); $textColor = imagecolorallocate($this->image, 0, 0, 0); $font = __DIR__ . '/arial.ttf'; for ($i = 0; $i < $this->length; $i++) { $x = ($this->width - 20) / $this->length * $i + 10; $y = $this->height / 2 + 10; imagettftext($this->image, 20, rand(-10, 10), $x, $y, $textColor, $font, $code[$i]); } header('Content-Type: image/png'); imagepng($this->image); } // 验证码校验 public function check($code) { if (strtolower($code) == strtolower($_SESSION['captcha'])) { return true; } else { return false; } } // 生成随机字符 protected function generateCode() { $code = ''; $charsetLength = strlen($this->charset); for ($i = 0; $i < $this->length; $i++) { $code .= $this->charset[rand(0, $charsetLength - 1)]; } $_SESSION['captcha'] = $code; return $code; } // 析构函数 public function __destruct() { imagedestroy($this->image); } } ``` 使用示例: ```php $captcha = new Captcha(); ``` 这将生成一个验证码图像并将其输出到浏览器。要检查用户输入的验证码是否正确,可以调用`check()`方法: ```php if ($captcha->check($_POST['captcha'])) { // 验证码正确 } else { // 验证码错误 } ``` 请注意,此示例仅用于演示目的。在实际应用程序中,您可能需要添加其他功能(例如:检查用户是否已经提交了表单,以防止滥用)并进行更多的安全检查。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值