有膽量你就來跟著路老師卷起來!?-- 純干貨,技術知識分享
路老師給大家分享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?高級應用
?