session有幾種使用方式
1.基於cookie的
2.基於url的sid字串的
3.儲存到資料庫中的
4.儲存的memcache中的(效率最好的)
寫了一個登陸的小例子:
具體實現如下:
comm.php
<?php/** * 當瀏覽器禁用掉cookie之後,可以採取傳遞sessionID */session_start();echo "id:" . session_id() . "<br>";//判斷使用者是否登入,如果未登入實現跳轉if(!$_SESSION["isLogin"]){header("Location:login.php");}?>
conn.inc.php
<?php$mysqli=new mysqli("localhost", "root", "root", "phpdb");?>
control.php
<?phpinclude "conn.inc.php";echo "你的許可權如下:<br>";$sql="select allow_1, allow_2, allow_3, allow_4 from users where id='{$_SESSION["uid"]}'";$result=$mysqli->Query($sql);$user=$result->fetch_assoc();if($user["allow_1"]){echo "111111111111111111111111<br>";}if($user["allow_2"]){echo "2222222222222222<br>";}if($user["allow_3"]){echo "333333333333333333333<br>";}if($user["allow_4"]){echo "444444444444444444444444<br>";}?>
index.php
<?phpinclude "comm.php";echo "使用者<b>".$_SESSION["username"]."</b>您好, 這是網站這首頁。";include "control.php";?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><a href="two.php?sid=<?php echo session_id()?>">第二頁</a> <br><a href="three.php?sid=<?php echo session_id()?>">第三頁</a> <br><a href="logout.php?sid=<?php echo session_id()?>">退出</a> <br>
login.php
<?phpsession_start();echo "id:" . session_id() . "<br>";if (isset($_POST["sub"])) { include "conn.inc.php"; $sql = "select id from users where name='{$_POST["name"]}' and password='" . md5($_POST["password"]) . "'"; $result = $mysqli->query($sql); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); $_SESSION["username"] = $_POST["name"]; $_SESSION["uid"] = $row["id"]; $_SESSION["isLogin"] = 1; //跳轉到index.php echo '<script>'; //1.自訂的參數名sid;需要在其他頁面開啟session的時候先session_id($_GET["sid"]);// echo "location='index.php?sid=".session_id()."'"; //2.或者使用常量SID來替換掉PHPSESSDI=xxxxxxxxxxxx //好處是:如果cookie開啟SID為空白,如果cookie未開啟,則採用SID //3.或者修改php.ini的session.use_trans_sid=1 //這樣只有在php中的跳轉需要加上SID,其他的頁面跳轉不需要加SID了 //4. echo "location='index.php?PHPSESSID=".session_id()."'"; //一般是基於3的方式 echo '</script>'; }else{ echo "使用者名稱密碼有誤。<br>"; }}?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><head><title>使用者登入</title></head><body><form action="login.php?PHPSESSID=<?php echo session_id()?>" method="post"><table align="center" border="1" width="300"><caption>使用者登入</caption><tr><th>使用者名稱</th><td><input type="text" name="name"></td></tr><tr><th>密 碼</th><td><input type="password" name="password"></td></tr><tr><td colspan="2" align="center"><input type="submit" name="sub"value="登 錄"></td></tr></table></form></body></html>
logout.php
<?phpinclude "comm.php";$username = $_SESSION["username"];destroySession();$_SESSION["username"]=$username;echo $username . "再見!";/** * 銷毀session */function destroySession(){//1.開啟session//在新頁面中需要先開啟session//session_start();//2.刪除資料//刪除session中的值方法一:unset($_SESSION["aa"]);//刪除session中的值方法二:$_SESSION=array();//3.刪除用戶端在COOKIE中 sessionIDif (isset($_COOKIE[session_name()])){//需要指定cookie的路徑在php.ini中session.cookie_pathsetcookie(session_name(),'',time()-3600,'/');};//4.徹底銷毀sessionsession_destroy();}?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><br><a href="login.php">重新登入</a>
three.php
<?phpinclude "comm.php";echo "使用者<b>".$_SESSION["username"]."</b>您好, 這是網站這三個個頁面。";include "control.php";?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><a href="two.php?PHPSESSID=<?php echo session_id()?>">第二頁</a> <br><a href="three.php?PHPSESSID=<?php echo session_id()?>">第三頁</a> <br><a href="logout.php?PHPSESSID=<?php echo session_id()?>">退出</a> <br>
two.php
<?phpinclude "comm.php";echo "使用者<b>".$_SESSION["username"]."</b>您好, 這是網站這二個頁面。";include "control.php";?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><a href="two.php?PHPSESSID=<?php echo session_id()?>">第二頁</a> <br><a href="three.php?PHPSESSID=<?php echo session_id()?>">第三頁</a> <br><a href="logout.php?PHPSESSID=<?php echo session_id()?>">退出</a> <br>