PHP模拟登录带验证码是指使用PHP编写脚本,模拟浏览器行为进行网站登录,并处理验证码验证的过程。验证码(CAPTCHA)是一种区分用户是计算机还是人的程序,通常用于防止自动化程序的恶意行为,如暴力破解密码、垃圾邮件发送等。
以下是一个简单的PHP模拟登录带图片验证码的示例:
<?php
session_start();
// 生成验证码
if (empty($_SESSION['captcha'])) {
$_SESSION['captcha'] = substr(md5(uniqid(mt_rand(), true)), 0, 5);
}
// 验证码图片
header('Content-type: image/png');
$image = imagecreatetruecolor(100, 30);
$bgColor = imagecolorallocate($image, 255, 255, 255);
$textColor = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, 100, 30, $bgColor);
imagestring($image, 5, 20, 8, $_SESSION['captcha'], $textColor);
imagepng($image);
imagedestroy($image);
// 处理登录请求
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['captcha'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$userCaptcha = $_POST['captcha'];
// 验证验证码
if (strtolower($userC奘aptcha) === strtolower($_SESSION['captcha'])) {
// 验证用户名和密码
if ($username === 'admin' && $password === '123456') {
echo "登录成功!";
} else {
echo "用户名或密码错误!";
}
} else {
echo "验证码错误!";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模拟登录</title>
</head>
<body>
<form method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
验证码:<input type="text" name="captcha">
<img src="login.php" alt="验证码"><br>
<input type="submit" value="登录">
</form>
</body>
</html>session_start()在脚本开头调用。通过以上步骤和示例代码,您可以实现一个基本的PHP模拟登录带验证码的功能。
没有搜到相关的文章