自学PHP的登录以及退出登录的超细致代码文件,仅供初学者学习,大神请批评…
前端登录页html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>用户登录</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<form action="输入你的跳转文件地址" class="login-form" method="post">
<h2>登录</h2>
<input type="text" name="username" placeholder="用户名"/>
<input type="password" name="password" placeholder="密码">
<button type="submit">登录</button>
</form>
</div>
</body>
</html>
前端登录页style.css代码
html,
body {
margin: 0;
font-family: "PingFang SC", "Microsoft Yahei",sans-serif;
}
.container {
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: url("https://images.unsplash.com/photo-1542273917363-3b1817f69a2d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=3453&q=80.jfif")
fixed no-repeat;
background-size: cover;
}
.login-form {
width: 240px;
height: 220px;
display: flex;
flex-direction: column;
padding: 40px;
text-align: center;
position: relative;
z-index: 100;
background: inherit;
border-radius: 18px;
overflow: hidden;
}
.login-form::before {
content: "";
width: calc(100% + 20px);
height: calc(100% + 20px);
position: absolute;
top: -10px;
left: -10px;
overflow: hidden;
background: inherit;
box-shadow: inset 0 0 0 200px rgba(255, 255, 255, 0.25);
filter: blur(6px);
z-index: -1;
}
.login-form h2 {
font-size: 18px;
font-weight: 400;
color: #3d5245;
}
.login-form input,
.login-form button {
margin: 6px 0;
height: 36px;
border: none;
background-color: rgba(255, 255, 255, 0.3);
border-radius: 4px;
padding: 0 14px;
color: #3d5245;
}
.login-form input::placeholder {
color: #3d5245;
}
.login-form button:focus,
.login-form input:focus {
outline: 0;
}
.login-form button {
margin-top: 24px;
background-color: rgba(57, 88, 69, 0.4);
color: white;
position: relative;
overflow: hidden;
cursor: pointer;
transition: 0.4s;
}
.login-form button:hover {
background-color: rgba(12, 80, 38, 0.67);
}
.login-form button:focus {
outline: 0;
}
.login-form button::before,
.login-form button::after {
content: "";
display: block;
width: 80px;
height: 100%;
background: rgba(179, 255, 210, 0.5);
opacity: 0.5;
position: absolute;
top: 0;
left: 0;
transform: skewX(-15deg);
filter: blur(30px);
overflow: hidden;
transform: translateX(-100px);
}
.login-form button::after {
width: 40px;
background: rgba(179, 255, 210, 0.3);
left: 60px;
opacity: 0;
filter: blur(5px);
}
.login-form button:hover::before {
transition: 1s;
transform: translateX(320px);
opacity: 0.7;
}
.login-form button:hover::after {
transition: 1s;
transform: translateX(320px);
opacity: 1;
}
登录后台代码
<?php
session_start();
if(!isset($_SESSION['UserInfo'])){
header('location:输入你的跳转文件地址');
exit;
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>这是商城的后台</title>
</head>
<body>
<h1>这是商城的后台!</h1>
<h2><a href="输入你的跳转文件地址">退出登录</h2>
</body>
</html>
登录的逻辑运算代码
<?php
session_start();
$UserInfo = [
'username' => 'WiFiMing',
'password' => '123456',
];
$username = '';
$password = '';
if (isset($_POST['username'])){
$username = $_POST['username'];
}
if (isset($_POST['password'])){
$password = $_POST['password'];
}
if($username != $UserInfo['username']){
var_dump('您没有注册,请注册后使用!');
header('location:输入你的跳转地址及参数');
exit;
}
if($password != $UserInfo['password']){
var_dump('您的密码输入错误!');
header('location:输入你的跳转地址及参数');
exit;
}
$_SESSION['UserInfo'] = $UserInfo;
echo '我已经获取到了登录信息:';
print_r($_SESSION['UserInfo']);
header('location:你的登录成功的跳转地址');
echo "<h1>这是登录的逻辑运算代码页</h1>";
exit;
退出登录
<?php
session_start();
session_destroy();
echo '退出成功!';
header('location:退出登录之后输入你的跳转地址');
|