有胆量你就来跟着路老师卷起来! -- 纯干货,技术知识分享
路老师给大家分享PHP语言的知识了,旨在想让大家入门PHP,并深入了解PHP语言。
上文讲述了cookie的概念,创建,获取,销毁以及生命周期后,我们利用本文来实现一个小的案例,实现七天免登录的案例。
七天免登录功能案例
1 创建users用户表,并添加一个用户。
CREATE TABLE `users` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 其中密码是md5处理过的密文
insert into `users` values('1','test','098f6bcd4621d373cade4e832627b4f6');
2 创建登录页面login.php:
<!DOCTYPE html>
<html lang="en" class="is-centered is-bold">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录页面</title>
<link href="css/login.css" rel="stylesheet">
</head>
<body>
<section style="background: transparent;">
<form class="box py-3 px-4 px-2-mobile" role="form" method="post" action="checklogin.php" onsubmit="return check()" >
<div class="is-flex is-column is-justified-to-center">
<h1 class="title is-3 mb-a has-text-centered">
登录
</h1>
<div class="inputs-wrap py-3">
<div class="control">
<input class="input" type="text" id="username" name="username" placeholder="用户名" required></input>
</div>
<div class="control">
<input class="input" type="password" id="password" name="password" placeholder="密码" required></input>
</div>
<div class="control">
<button class="button is-submit is-primary is-outlined" type="submit">
提交
</button>
</div>
</div>
<footer class="footer-box">
<div style="display: inline-block">
<input type="checkbox" name="keep" id="keep" checked value="">七天免登录
</div>
<a href="register.html">
暂无账号,点击去登陆
</a>
</footer>
</div>
</form>
</section>
<script>
function check(){
if(document.getElementById("keep").checked){
document.getElementById("keep").value =1;
}
}
</script>
</body>
</html>
其中内部样式表login.css如下:
.title{
color:rgb(116, 116, 116);
font-size: 32px;
text-align: center;
}
.box{
width: 300px;
height: 150px;
background-color: #00f7ff;
}
.py-3{
width: 200px;
height: 30px;
background-color: pink;
}
.is-flex{
width: 300px;
height: 350px;
border: 15px solid #02632c;
background-color: rgb(255, 255, 255);
}
.inputs-wrap{
margin: 0 auto;
}
.control .input{
width: 200px;
height: 26px;
margin-bottom: 25px;
border: 1px solid #02632c;
border-radius: 5px;
}
.control button{
width: 200px;
border-radius: 5px;
border: 1px solid #9fb9aa;
font-size: 16px;
color: #363636;
background-color: #b1f3cd;
margin-left: 2px;
}
.footer-box{
width: 240px;
height: 100px;
margin-top: 110px;
margin-left: 45px;
color: #02632c;
font-size: 12px;
}
.footer-box input{
float: left;
}
.footer-box a{
float: right;
font-size: 12px;
color: rgb(248, 91, 91);
text-decoration: none;
}
登录页面效果如下:
3 创建checkLogin.php
接下来实现登录成功的检测,创建checkLogin.php用来检测:
<?php
if(isset($_POST['username']) && isset($_POST['password'])){
$username = trim($_POST['username']);
$password = md5(trim($_POST['password']));
require "config.php";
try {
$pdo = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER,DB_PWD);
} catch (PDOException $e) {
echo $e->getMessage();
}
$sql = 'select * from users where username = :username and password = :password';
$result = $pdo->prepare($sql);
$result->bindParam(':username',$username);
$result->bindParam(':password',$password);
if($result->execute()){
$rows=$result->fetch(PDO::FETCH_ASSOC);
if($rows){
if(isset($_POST['keep'])){
//勾选七天免登录
setcookie('username',$rows['username'],time()+604800);
}else{
setcookie('username',$rows['username']);
}
echo "<script>alert('恭喜您,登录成功!');
window.location.href='index.php';</script>";
}else{
echo "<script>alert('用户名或密码错误,登录失败!');
history.back();</script>";
exit();
}
}else{
echo "<script>window.location.href='login.php';</script>";
}
}
?>
checkLogin.php的config.php配置文件也给贴下面(不要嫌弃我啰嗦哦):
<?php
define('DB_HOST','localhost');
define('DB_USER','root');//你的数据库服务器登录用户
define('DB_PWD','passwd');//替换成你的数据库服务器密码
define('DB_NAME','db_test');//替换成你的数据库名
define('DB_PORT','3306');
define('DB_TYPE','mysql');
define('DB_CHARSET','utf8');
define('DB_DSN',"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET);
?>
此时你可以进行登录检测了。故意输入错误,会提示如下内容:
4 登录成功的界面index.php:
<?php
date_default_timezone_set('PRC');
//如果cookie不存在,那就是第一次访问网站
if(!isset($_COOKIE["username"])){
echo "<script>alert('请先登录');
window.location.href='login.php';</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>欢迎界面</title>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="container">
<div class="jumbotron" style="background-color:#17ecf1">
<h1>欢迎
<span style="color:white;font-weight:700">
<?php echo $_COOKIE['username']; ?>
</span>
登录网站
</h1>
<p><a class="btn btn-warning btn-lg" href="logout.php" role="button">退出登录</a></p>
</div>
</body>
</html>
我们测试一下,登录成功后的效果,此时你拷贝这个连接到新开一个浏览器页面,会出现不需要登录就能成功进入主页。
顺手把退出登录的功能也给实现了logout.php:
<?php
setcookie("username","",time()-1);
echo "<script>window.location.href='login.php';</script>";
?>
此文到此接触!
下一篇 Session