当前位置:网站首页>A solution for PHP to implement image login verification code
A solution for PHP to implement image login verification code
2022-06-12 05:25:00 【Missed engraving sometimes】
PHP Solution to realize image login verification code
HTML Code
<div class="login">
<p><input type="text" id="username" name="username" placeholder=" user name " autocomplete="off"></p>
<p><input type="password" id="password" name="username" placeholder=" password " autocomplete="off"></p>
<p><input type="text" id="captcha" name="captcha" placeholder=" Verification Code " maxlength="4" autocomplete="off"></p>
<p><img src="?m=Login&a=loginAccess&act=getCode" id="getCode" alt="" title=" Click refresh verification code "></p>
<p><button id="loginBtn"> Sign in </button></p>
</div>
Refresh the picture
$("#getCode").click(function () {
$(this).attr("src", '?m=Login&a=loginAccess&act=getCode&' + Math.random());
});
Generate verification code and picture
case "getCode";
require_once "libs/vcode.class.php";
$obj = new vcode();// Instantiation ;
SetCookie("authcode", $obj->authcode, time() + 30, "/");
die($obj->output());
break;
Verification code class library
class vcode
{
public $authcode = ''; // Verification Code
private $width = ''; // The verification code picture is wide
private $height = ''; // The verification code picture is high
private $len = ''; // Length of verification code
private $tilt = array(-30, 30); // Verification code tilt angle
private $font = 'AlteHaasGroteskBold.ttf'; // The font file
private $str = ''; // Verification code base
private $im = ''; // Handle to generate picture
// Constructors , Generate verification code .
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)];
}
}
// Create a picture
private function imagecreate()
{
$this->im = imagecreatetruecolor($this->width, $this->height);
}
// Interference color
private function ext_color()
{
return imagecolorallocate($this->im, rand(50, 180), rand(50, 180), rand(50, 180));
}
// Create interference points
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());
}
}
// Create interference lines
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());
}
}
// Write the verification code into the picture ( Unable to join $this->imgstrfloat() Use at the same time )
private function imgstr()
{
$old_x = 1;
for ($i = 0; $i < $this->len; $i++) {
$fontsize = rand(2, 5); // font size
$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);
}
}
// Write the verification code obliquely into the picture ( Unable to join $this->imgstr() Use at the same time )
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); // font size
$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]);
}
}
// Output pictures
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
边栏推荐
- Can‘t find a suitable configuration file in this directory or any parent. Error reporting and resolution
- Detailed usage of vim editor
- 44. a digit in a sequence of digits
- CentOS compiling and installing mysql8.0
- Yolo opencv scale identification scale reading identification water gauge identification water level identification source code
- A complete set of installation procedures (for learning and communication only)
- Chapter 1
- Servlet core
- Matlab: image rotation and interpolation and comparison of MSE before and after
- Variables and data types
猜你喜欢

Multi thread learning III. classification of threads

Detailed analysis of the 2021 central China Cup Title A (color selection of mosaic tiles)

Codis 3. X expansion and contraction

Can‘t find a suitable configuration file in this directory or any parent. Error reporting and resolution
![[GIS tutorial] ArcGIS for sunshine analysis (with exercise data download)](/img/60/baebffb2024ddf5f2cb070f222b257.jpg)
[GIS tutorial] ArcGIS for sunshine analysis (with exercise data download)

Detailed analysis of mathematical modeling problem a (vaccine production scheduling problem) of May Day cup in 2021

BI 如何让SaaS产品具有 “安全感”和“敏锐感”(上)

Matlab: image rotation and interpolation and comparison of MSE before and after

Harris corner detection principle-

Three. JS import model demo analysis (with notes)
随机推荐
Harris corner detection principle-
Chrome is amazingly fast, fixing 40 vulnerabilities in less than 30 days
When the build When gradle does not load the dependencies, and you need to add a download path in libraries, the path in gradle is not a direct downloadable path
Esp32-who face detection
Automated test - dark horse headline test project
week7
Google reinforcement learning framework seed RL environment deployment
51. reverse order pairs in the array
Pupanvr- establishment of development environment and diary, addition of some basic tool functions (3)
Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class
The master programmer "plays" a C program that is not like C
Stm32f4 ll library multi-channel ADC
Automated testing - Po mode / log /allure/ continuous integration
12.24 day exercise -- Programming summation, 99 multiplication table, while loop and for loop exercises
Matlab: image rotation and interpolation and comparison of MSE before and after
Static keyword and inheritance, polymorphic and special classes
Classes and objects, methods and encapsulation
Calculation method notes for personal use
The server time zone value ‘Ö Ð¹ ú±ê ×¼ ʱ ¼ ä‘ is unrecognized or represents more than one time zone. You
个体工商户是不是法人企业