用layui开发一个登录页面,包含用户名 密码 验证码 单位号 标题叫 登录,登录失败要有提示,登录成功跳转到index.html页面
时间: 2023-11-17 14:05:28 浏览: 90
设计的一个登录页面(html)
5星 · 资源好评率100%
首先,你需要在HTML文件中引入Layui的样式和JavaScript:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录页面</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/layui/2.5.6/css/layui.css">
</head>
<body>
<div class="layui-container">
<div class="layui-row">
<div class="layui-col-md-offset3 layui-col-md6">
<form class="layui-form" action="">
<div class="layui-form-item">
<label class="layui-form-label">用户名</label>
<div class="layui-input-block">
<input type="text" name="username" required lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">密码</label>
<div class="layui-input-block">
<input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">验证码</label>
<div class="layui-input-block">
<input type="text" name="captcha" required lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input layui-inline">
<img id="captchaImg" src="captcha.php" onclick="this.src='captcha.php?'+Math.random();" alt="验证码" title="点击刷新">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">单位号</label>
<div class="layui-input-block">
<input type="text" name="company" required lay-verify="required" placeholder="请输入单位号" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit="" lay-filter="login">登录</button>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.6/layui.js"></script>
<script>
// layui表单组件
layui.use(['form'], function(){
var form = layui.form;
// 监听表单提交
form.on('submit(login)', function(data){
// 模拟请求
if (data.field.username === 'admin' && data.field.password === '123456' && data.field.captcha === 'abcd' && data.field.company === '123') {
layer.msg('登录成功', {icon: 1});
setTimeout(function() {
window.location.href = 'index.html';
}, 1000);
} else {
layer.msg('用户名或密码错误', {icon: 2});
}
return false;
});
});
</script>
</body>
</html>
```
以上代码实现了一个简单的登录页面,包含用户名、密码、验证码和单位号。当用户点击“登录”按钮时,会模拟提交表单数据,并根据用户名、密码、验证码和单位号判断登录是否成功。如果登录成功,则在1秒后跳转到index.html页面;否则,会弹出提示框提示用户名或密码错误。
其中,验证码的生成和刷新是通过调用captcha.php脚本来实现的。你需要在服务器上放置一个captcha.php文件,代码如下:
```php
<?php
header('Content-type: image/png');
$width = 80;
$height = 30;
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);
$code = '';
for ($i = 0; $i < 4; $i++) {
$font = rand(3, 5);
$x = $i * ($width / 4) + rand(0, 10);
$y = rand(0, $height / 2 - 5);
$color = imagecolorallocate($image, rand(0, 150), rand(0, 150), rand(0, 150));
$char = chr(rand(65, 90));
$code .= $char;
imagettftext($image, $font, rand(-30, 30), $x, $y, $color, 'arial.ttf', $char);
}
$_SESSION['captcha'] = $code;
imagepng($image);
imagedestroy($image);
```
以上代码会生成一个包含4个大写字母的验证码,并将验证码保存在会话中。每次刷新验证码时,都会重新生成一个新的验证码。
阅读全文