掌握cookie和session的回話流程:登錄:login.php——>驗(yàn)證:check.php——>后臺(tái)頁(yè)面:admin.php——>退出登錄:logout——>退出成功后返回到登錄界面:login.php.
cookie(沒(méi)有使用bootstrap美化):
login.php:
<?php if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin'): ?> <script>alert('您已經(jīng)登錄了,請(qǐng)不要重復(fù)登錄');location.assign('admin.php');</script> <?php endif; ?> <!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> <h3>用戶登錄</h3> <form action="check.php" method="post" onsubmit="return isEmpty();"> <P> <label for="email">郵箱:</label> <input type="email" name="email" id="email"> </P> <P> <label for="password">提交:</label> <input type="password" name="password" id="password"> </P> <p> <button>提交</button> </p> </form> <script> // onsubmit: 表單提交前進(jìn)行驗(yàn)證, 默認(rèn)返回true function isEmpty() { var email = document.getElementById('email').value; var password = document.getElementById('password').value; // 用if 語(yǔ)句來(lái)做非空驗(yàn)證函數(shù) // if (email.length === 0 || password.length === 0) { // alert('郵箱或密碼不能為空'); // return false; // } if (email.length ===0) { alert('郵箱不能為空'); if (password.length === 0 ) { alert('密碼不能為空'); } return false; } } </script> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
check.php:
<?php if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin') { echo '<script>alert(\'您已經(jīng)登錄了,請(qǐng)不要重復(fù)登錄\');location.assign("admin.php");</script>'; } //j檢查是否獲得提交數(shù)據(jù) //print_r($_POST); //連接數(shù)據(jù)庫(kù) require __DIR__ . '/inc/connect.php'; //var_dump($pdo); //獲取數(shù)據(jù),并創(chuàng)建新變量 $email = $_POST['email']; $password = sha1($_POST['password']); //var_dump($email,$password); //用user表進(jìn)行驗(yàn)證 $sql = 'SELECT * FROM `user` WHERE `email`=:email AND `password`=:password LIMIT 1'; //創(chuàng)建SQL語(yǔ)句模板對(duì)象,預(yù)處理對(duì)象 $stmt = $pdo->prepare($sql); //執(zhí)行,execute()內(nèi)為數(shù)據(jù)綁定 $stmt->execute(['email'=>$email, 'password'=>$password]); //fetch(): 成功返回?cái)?shù)組,失敗返回false $user = $stmt->fetch(PDO::FETCH_ASSOC); //var_dump($user); if (false === $user) { //登錄失敗 echo '<script>alert("登錄失敗,請(qǐng)檢查"); location.href="login.php";</script>'; //終止當(dāng)前腳本 die; //exit; exit(); } //登錄成功,將用戶信息寫(xiě)入cookie setcookie('username',$user['username']); echo '<script>alert(\'登錄成功\'); location.assign("admin.php");</script>';
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
admin.php:
<?php if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin'): ?> <!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>后臺(tái)首頁(yè)</title> </head> <body> <h1>后天首頁(yè)</h1> <p> <?php echo $_COOKIE['username'] ?> </p> <p> <a href="logout.php">退出</a> </p> </body> </html> <?php else: ?> <script>alert('請(qǐng)先登錄');location.assign('login.php');</script>; <?php endif ?>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
logout.php:
<?php //必須在已經(jīng)登錄的情況下,才能進(jìn)行退出登錄的操作. //isset():判斷當(dāng)前變量是否存在 if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin') { //退出 setcookie('username',null,time()-3600); echo '<script> alert(\'退出成功\');location.assign("login.php");</script>'; } else { echo '<script> alert(\'請(qǐng)先登錄\');location.assign("login.php");</script>'; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
session改寫(xiě):
<?php //開(kāi)啟回話 session_start(); //向cookie中寫(xiě)入session_id:PHPSESSID if (isset($_SESSION['username']) && $_SESSION['username'] === 'admin'): ?> <script>alert('您已經(jīng)登錄了,請(qǐng)不要重復(fù)登錄');location.assign('admin.php');</script> <?php endif; ?> <!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"> <link rel="stylesheet" href="static/css/bootstrap.css"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>用戶登錄</title> </head> <body> <h3>用戶登錄</h3> <form action="check.php" method="post" onsubmit="return isEmpty();"> <!-- <P>--> <!-- <label for="email">郵箱:</label>--> <!-- <input type="email" name="email" id="email">--> <!-- </P>--> <!-- <P>--> <!-- <label for="password">密碼:</label>--> <!-- <input type="password" name="password" id="password">--> <!-- </P>--> <p> <label class="input-group"> <span class="input-group-addon" id="basic-addon1">郵 箱:</span> <input id="email" name="email" type="text" placeholder="請(qǐng)您輸入郵箱地址" aria-describedby="basic-addon1"> </label> </p> <p> <label class="input-group"> <span class="input-group-addon" id="basic-addon1">密 碼:</span> <input id="password" name="password" type="text" placeholder="請(qǐng)輸入您的秘密" aria-describedby="basic-addon1"> </label> </p> <!-- <P>--> <!-- <label for="password">提交:</label>--> <!-- <input type="password" name="password" id="password">--> <!-- </P>--> <p> <button class="btn btn-success">提交</button> </p> </form> <script> // onsubmit: 表單提交前進(jìn)行驗(yàn)證, 默認(rèn)返回true function isEmpty() { var email = document.getElementById('email').value; var password = document.getElementById('password').value; // 用if 語(yǔ)句來(lái)做非空驗(yàn)證函數(shù) // if (email.length === 0 || password.length === 0) { // alert('郵箱或密碼不能為空'); // return false; // } if (email.length ===0) { alert('郵箱不能為空'); if (password.length === 0 ) { alert('密碼不能為空'); } return false; } } </script> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
頁(yè)面顯示效果:
check.php:
<?php //開(kāi)啟回話 session_start(); //向cookie中寫(xiě)入session_id:PHPSESSID if (isset($_SESSION['username']) && $_SESSION['username'] === 'admin') { echo '<script>alert(\'您已經(jīng)登錄了,請(qǐng)不要重復(fù)登錄\');location.assign("admin.php");</script>'; } //j檢查是否獲得提交數(shù)據(jù) //print_r($_POST); //連接數(shù)據(jù)庫(kù) require __DIR__ . '/inc/connect.php'; //var_dump($pdo); //獲取數(shù)據(jù),并創(chuàng)建新變量 $email = $_POST['email']; $password = sha1($_POST['password']); //var_dump($email,$password); //用user表進(jìn)行驗(yàn)證 $sql = 'SELECT * FROM `user` WHERE `email`=:email AND `password`=:password LIMIT 1'; //創(chuàng)建SQL語(yǔ)句模板對(duì)象,預(yù)處理對(duì)象 $stmt = $pdo->prepare($sql); //執(zhí)行,execute()內(nèi)為數(shù)據(jù)綁定 $stmt->execute(['email'=>$email, 'password'=>$password]); //fetch(): 成功返回?cái)?shù)組,失敗返回false $user = $stmt->fetch(PDO::FETCH_ASSOC); //var_dump($user); if (false === $user) { //登錄失敗 echo '<script>alert("登錄失敗,請(qǐng)檢查"); location.href="login.php";</script>'; //終止當(dāng)前腳本 die; //exit; exit(); } //登錄成功,將用戶信息寫(xiě)入cookie //setcookie('username',$user['username']); $_SESSION['username'] = $user['username']; //session文件中的值:username|s:5:"admin"; echo '<script>alert(\'登錄成功\'); location.assign("admin.php");</script>';
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
彈窗顯示效果:
admin.php:
<?php //開(kāi)啟回話 session_start(); if (isset($_SESSION['username']) && $_SESSION['username'] === 'admin'): ?> <!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>后臺(tái)首頁(yè)</title> </head> <body> <h1>后天首頁(yè)</h1> <p> <?php echo $_SESSION['username'] ?> </p> <p class="btn btn-success"> <a href="logout.php">退出</a> </p> </body> </html> <?php else: ?> <script>alert('請(qǐng)先登錄');location.assign('login.php');</script>; <?php endif ?>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
后臺(tái)頁(yè)面的顯示效果:
logout.php:
<?php //開(kāi)啟回話 session_start(); //必須在已經(jīng)登錄的情況下,才能進(jìn)行退出登錄的操作. //isset():判斷當(dāng)前變量是否存在 if (isset($_SESSION['username']) && $_SESSION['username'] === 'admin') { //退出 //將服務(wù)器中對(duì)應(yīng)的session文件刪除 session_destroy(); setcookie('username',null,time()-3600); echo '<script> alert(\'退出成功\');location.assign("login.php");</script>'; } else { echo '<script> alert(\'請(qǐng)先登錄\');location.assign("login.php");</script>'; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
退出的彈窗效果:
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)