当前位置:网站首页>PHP实现图片登录验证码的解决方案
PHP实现图片登录验证码的解决方案
2022-06-12 05:24:00 【漏刻有时】
PHP实现图片登录验证码的解决方案
HTML代码
<div class="login">
<p><input type="text" id="username" name="username" placeholder="用户名" autocomplete="off"></p>
<p><input type="password" id="password" name="username" placeholder="密码" autocomplete="off"></p>
<p><input type="text" id="captcha" name="captcha" placeholder="验证码" maxlength="4" autocomplete="off"></p>
<p><img src="?m=Login&a=loginAccess&act=getCode" id="getCode" alt="" title="点击刷新验证码"></p>
<p><button id="loginBtn">登录</button></p>
</div>
刷新图片
$("#getCode").click(function () {
$(this).attr("src", '?m=Login&a=loginAccess&act=getCode&' + Math.random());
});
生成验证码和图片
case "getCode";
require_once "libs/vcode.class.php";
$obj = new vcode();//实例化;
SetCookie("authcode", $obj->authcode, time() + 30, "/");
die($obj->output());
break;
验证码类库
class vcode
{
public $authcode = ''; //验证码
private $width = ''; //验证码图片宽
private $height = ''; //验证码图片高
private $len = ''; //验证码长度
private $tilt = array(-30, 30); //验证码倾斜角度
private $font = 'AlteHaasGroteskBold.ttf'; //字体文件
private $str = ''; //验证码基
private $im = ''; //生成图片的句柄
//构造函数,生成验证码。
function __construct($width = 100, $heigh = 40, $len = 4)
{
$this->width = $width;
$this->height = $heigh;
$this->len = $len;
//$this->str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$this->str = '0123456789';
$str_len = strlen($this->str) - 1;
for ($i = 0; $i < $len; $i++) {
$this->authcode .= $this->str[rand(0, $str_len)];
}
}
//创建图片
private function imagecreate()
{
$this->im = imagecreatetruecolor($this->width, $this->height);
}
//干扰颜色
private function ext_color()
{
return imagecolorallocate($this->im, rand(50, 180), rand(50, 180), rand(50, 180));
}
//创建干扰点
private function ext_point()
{
for ($i = 0; $i < $this->width * 2; $i++) {
imagesetpixel($this->im, rand(1, $this->width - 1), rand(1, $this->height - 1), $this->ext_color());
}
}
//创建干扰线
private function ext_line()
{
for ($i = 0; $i < $this->len; $i++) {
$x1 = rand(1, $this->width - 1);
$y1 = rand(1, $this->height - 1);
$x2 = rand(1, $this->width - 1);
$y2 = rand(1, $this->height - 1);
imageline($this->im, $x1, $y1, $x2, $y2, $this->ext_color());
}
}
//把验证码写入图片(不能和$this->imgstrfloat()同时使用)
private function imgstr()
{
$old_x = 1;
for ($i = 0; $i < $this->len; $i++) {
$fontsize = rand(2, 5); //字体大小
$tmp_1 = $fontsize * 2.5;
$tmp_2 = $i > 0 ? $tmp_1 : 0;
$y = rand(1, $this->height / 2);
$x = rand($old_x + $tmp_2, ($i + 1) * ($this->width) / $this->len - $tmp_1);
$old_x = $x;
$color = imagecolorallocate($this->im, rand(200, 255), rand(200, 255), rand(200, 255));
imagestring($this->im, $fontsize, $x, $y, $this->authcode[$i], $color);
}
}
//把验证码倾斜写入图片(不能和$this->imgstr()同时使用)
private function imgstrfloat()
{
$old_x = 1;
for ($i = 0; $i < $this->len; $i++) {
$fontfloat = rand($this->tilt[0], $this->tilt[1]);
$fontsize = rand(10, 15); //字体大小
$tmp_1 = $i > 0 ? $fontsize : 0;
$y = rand($fontsize + 2, $this->height - 2);
$x = rand($old_x + $tmp_1 + 2, ($i + 1) * ($this->width) / $this->len - $fontsize - 2);
$old_x = $x;
$color = imagecolorallocate($this->im, rand(200, 255), rand(200, 255), rand(200, 255));
imagettftext($this->im, $fontsize, $fontfloat, $x, $y, $color, $this->font, $this->authcode[$i]);
}
}
//输出图片
public function output()
{
$this->imagecreate();
$this->imgstr();
//$this->imgstrfloat();
$this->ext_point();
$this->ext_line();
header('content-type:image/png');
imagepng($this->im);
imagedestroy($this->im);
}
}
@lockdata.cn
边栏推荐
- Token based authentication
- Layer sublayer assigns values to the page elements of the parent layer to achieve the effect of transferring values to the page of the parent layer
- 12.26 exercise summary
- Object class not ended
- cellular automaton
- 62. the last number left in the circle
- Spatial distribution data of China's tertiary watershed / national new area distribution data /npp net primary productivity data / spatial distribution data of vegetation cover / land use data /ndvi d
- 4.3 simulate browser operation and page waiting (display waiting and implicit waiting, handle)
- Spatial distribution data of national multi-year average precipitation 1951-2021, temperature distribution data, evapotranspiration data, evaporation data, solar radiation data, sunshine data and wind
- Multi thread learning III. classification of threads
猜你喜欢

Matlab: halftone and dither conversion

Normalized vegetation index (NDVI) data, NPP data, GPP data, evapotranspiration data, vegetation type data, ecosystem type distribution data

It costs less than 30 yuan, but we still don't build it quickly - check the small knowledge of software application

Multi thread learning III. classification of threads

How to generate provincial data from county-level data in ArcGIS?

Some problems of Qinglong panel

Quickly get PCA (principal component analysis) (principle code case)

How Bi makes SaaS products have a "sense of security" and "sensitivity" (Part I)

National land use data of 30m precision secondary classification

Some problems of silly girl solved
随机推荐
Alsa architecture application aplay and amixer call relationship (from application layer to kernel driver)
Project requirements specification
@Configurationproperties value cannot be injected
[C language] realize string interception function
[backtracking based on bit operation] queen n problem 2
Summary of problems in rv1109/rv1126 product development
Classes and objects, methods and encapsulation
[backtracking] backtracking to solve subset problems
Layer sublayer assigns values to the page elements of the parent layer to achieve the effect of transferring values to the page of the parent layer
Chrome is amazingly fast, fixing 40 vulnerabilities in less than 30 days
New knowledge today
Link: fatal error lnk1168: cannot open debug/test Solution of exe for writing
Calculation method notes for personal use
The emergence of new ides and the crisis of programmers?
Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)
week7
[GIS tutorial] land use transfer matrix
Sv806 QT UI development
Drive safety coding & troubleshooting guide
Nbiot module me3616 at command mqtt connecting thingsboard