用html5写一个带有验证码的登录页
时间: 2023-09-30 11:07:30 浏览: 63
以下是一个基本的 HTML5 登录页面,包含了验证码功能:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<style type="text/css">
body {
background-color: #f4f4f4;
font-family: Arial, sans-serif;
}
.container {
margin: 50px auto;
width: 400px;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px #ccc;
}
h2 {
text-align: center;
margin-bottom: 20px;
}
input[type="text"], input[type="password"], input[type="submit"], input[type="reset"] {
display: block;
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: none;
border-radius: 3px;
box-shadow: 0 0 2px #ccc;
font-size: 14px;
color: #333;
}
input[type="submit"], input[type="reset"] {
background-color: #333;
color: #fff;
cursor: pointer;
transition: background-color 0.3s ease;
}
input[type="submit"]:hover, input[type="reset"]:hover {
background-color: #555;
}
#captcha {
margin-bottom: 15px;
}
#captcha img {
vertical-align: middle;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h2>登录</h2>
<form action="#" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<div id="captcha">
<label for="captcha_code">验证码:</label>
<input type="text" id="captcha_code" name="captcha_code" required>
<img src="captcha.php" alt="验证码" onclick="this.src='captcha.php?'+Math.random();">
</div>
<input type="submit" value="登录">
<input type="reset" value="重置">
</form>
</div>
</body>
</html>
```
在这个页面中,验证码是通过一个 PHP 脚本生成的,需要在同一目录下创建一个名为 `captcha.php` 的文件,代码如下:
```php
<?php
session_start();
// 随机生成验证码
$code = substr(md5(mt_rand()), 0, 4);
// 保存验证码到 session 中
$_SESSION['captcha'] = $code;
// 设置响应类型为 image/jpeg
header('Content-Type: image/jpeg');
// 创建图像
$image = imagecreate(60, 30);
// 设置背景色和文字颜色
$bgColor = imagecolorallocate($image, 255, 255, 255);
$textColor = imagecolorallocate($image, 0, 0, 0);
// 在图像上输出验证码
imagestring($image, 5, 10, 8, $code, $textColor);
// 输出图像
imagejpeg($image);
// 释放资源
imagedestroy($image);
?>
```
这个 PHP 脚本会生成一个 60 x 30 的图像,上面包含了一个随机生成的 4 位验证码,然后将验证码保存到 session 中,并输出图像到客户端。验证码图像可以通过添加一个 `<img>` 元素的方式在 HTML5 页面中显示,并在单击图像时刷新验证码。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)