结合HTML+CSS+jQuery实现登录界面,包含验证码
时间: 2023-09-23 21:09:52 浏览: 53
可以通过以下步骤实现:
1. 创建一个 HTML 页面,并添加所需的元素,如用户名、密码、验证码和登录按钮等。
```html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div class="login-container">
<h2>Login</h2>
<form>
<label for="username">Username</label>
<input type="text" id="username" name="username" required>
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<label for="captcha">Captcha</label>
<div class="captcha-container">
<input type="text" id="captcha" name="captcha" required>
<img src="captcha.php" alt="captcha">
</div>
<button type="submit" id="login-btn">Login</button>
</form>
</div>
</body>
</html>
```
2. 创建一个 CSS 文件,并添加所需的样式,如登录框的样式等。
```css
.login-container {
max-width: 400px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 0 10px #ccc;
background-color: #fff;
}
.login-container h2 {
text-align: center;
margin-bottom: 20px;
}
.login-container label {
display: block;
margin-bottom: 10px;
}
.login-container input[type="text"],
.login-container input[type="password"] {
display: block;
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
margin-bottom: 20px;
}
.captcha-container {
display: flex;
align-items: center;
margin-bottom: 20px;
}
.captcha-container img {
margin-left: 20px;
}
#login-btn {
display: block;
width: 100%;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: #fff;
font-size: 16px;
cursor: pointer;
}
```
3. 创建一个 PHP 文件,用于生成验证码图片。
```php
<?php
session_start();
$length = 6;
$width = 100;
$height = 40;
$characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$random_string = '';
for ($i = 0; $i < $length; $i++) {
$random_string .= $characters[rand(0, strlen($characters) - 1)];
}
$_SESSION['captcha'] = $random_string;
$image = imagecreatetruecolor($width, $height);
$bg_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, $width, $height, $bg_color);
imagettftext($image, 20, 0, 10, 30, $text_color, 'arial.ttf', $random_string);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
```
4. 创建一个 JavaScript 文件,用于验证用户输入的信息和提交表单。
```js
$(document).ready(function() {
$('form').submit(function(e) {
e.preventDefault();
var username = $('#username').val();
var password = $('#password').val();
var captcha = $('#captcha').val();
var captcha_session = '<?php echo $_SESSION["captcha"]; ?>';
if (username == '') {
alert('Please enter username.');
return;
}
if (password == '') {
alert('Please enter password.');
return;
}
if (captcha == '') {
alert('Please enter captcha.');
return;
}
if (captcha != captcha_session) {
alert('Invalid captcha.');
return;
}
$.ajax({
url: 'login.php',
type: 'POST',
data: {username: username, password: password},
success: function(response) {
if (response == 'success') {
alert('Login successful.');
location.reload();
} else {
alert('Login failed.');
}
}
});
});
});
```
5. 创建一个 PHP 文件,用于验证用户输入的信息和登录。
```php
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
if ($username == 'admin' && $password == '123456') {
$_SESSION['user'] = $username;
echo 'success';
} else {
echo 'error';
}
?>
```
以上是基本的实现方法,其中验证码生成使用了 PHP 的 GD 库。可以将代码部署在本地或者服务器上运行。