有胆量你就来跟着路老师卷起来! -- 纯干货,技术知识分享
路老师给大家分享PHP语言的知识了,旨在想让大家入门PHP,并深入了解PHP语言。
上一篇我们介绍了Session管理部分的概念,本文通过session来改写一些用户登录,此时的用户名存储到session里。
1 创建login2.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="checklogin2.php" ><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></div></form></section></body>
</html>
注意:上述代码种的样式表和Cookie免密登录的login.css相同。
2 数据提交到checkLogin2.php
单击提交之后,表单会提交到checkLogin2.php文件,并在该文件中处理登录逻辑。登录成功后,使用session_start()函数初始化Session变量,将username存储到Session中。
checkLogin2.php代码如下:(请仔细查看和cookie文章内容的区别)
<?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){//启动Sessionsession_start();$_SESSION['username']=$rows['username'];echo "<script>alert('恭喜您,登录成功!');window.location.href='index2.php';</script>";}else{echo "<script>alert('用户名或密码错误,登录失败!');history.back();</script>";exit();}}else{echo "<script>window.location.href='login2.php';</script>";}}?>
3 获取Session中的数据
登录成功后,username存储到Session中,可以使用$_SESSION['username']获取该值,在index2.php文件中,实现代码如下:
<?php
date_default_timezone_set('PRC');
//开启Session
session_start();
//如果Session不存在,那就是第一次访问网站
if(!isset($_SESSION["username"])){echo "<script>alert('请先登录');window.location.href='login2.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:#ff27d0"><h1>欢迎<span style="color:#363636;font-weight:700"><?php echo $_SESSION['username']; ?></span>登录网站</h1><p><a class="btn btn-warning btn-lg" href="logout2.php" role="button">退出登录</a></p></div>
</body>
</html>
4 退出登录logout2.php实现
<?php
//启动Sessionsession_start();//清除Sessionunset($_SESSION['username']);echo "<script>window.location.href='login2.php';</script>";
?>
此文到此接触!
下一篇 Session 高级应用