要制作验证码登录页面,可以按照以下步骤进行:
<?php
session_start();
$width = 100;
$height = 30;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$char_length = strlen($characters);
$code = '';
for ($i = 0; $i < 6; $i++) {
$code .= $characters[rand(0, $char_length - 1)];
}
$_SESSION['captcha_code'] = $code;
$image = imagecreatetruecolor($width, $height);
$bg_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $bg_color);
imagestring($image, 5, 40, 10, $code, $text_color);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Captcha Login</title>
</head>
<body>
<h2>Login Form</h2>
<form action="login.php" method="post">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required><br><br>
<label for="captcha">Captcha:</label>
<input type="text" name="captcha" id="captcha" required><br><br>
<img src="captcha.php" alt="captcha"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$captcha = $_POST['captcha'];
if ($username == 'admin' && $password == 'password' && $captcha == $_SESSION['captcha_code']) {
echo 'Login successful';
} else {
echo 'Login failed';
}
?>
通过以上步骤,就可以实现一个带有验证码的登录页面。当用户输入用户名、密码和验证码后,点击登录按钮,系统会校验用户输入的信息是否正确,并给出相应的提示。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: php explode()的用法是什么