課程115:使用 SESSION 來記錄 Web 應用程式的狀態
摘要:
HTTP 使用無連線狀態的連線(Stateless Connection)方式,然而,我們在開發應用程式時,常會需要在多個網頁,存取同一位使用者連線資料。PHP解決這個問題的方式,有以下幾種:
在這個課程中,我們著重在討論 Session的使用,並把 Session 應用在系統登入的實例中。
Session 變數的使用:
PHP 的全域變數 Session 提供給我們一個可以在多個頁面存取個別使用者連線資料的機制。對於針對個別使用者,需要在多個頁面存取共用的資料時,特別有用。如果,非由程式特別清除 Session 的話,個別使用者的 Session 資料會在特定時間(由 php.ini 設定)內,被系統清除掉。
以下,我們分別按照寫入Session、讀取Session、清除Session等方式,探討如何使用 Session 變數。
Session 的寫入:
範例碼:
<?php
// 1. 啟動 Session
session_start();
// 2. 登記 Session 變數名稱
session_register('foo');
// 3. 寫入 Session 變數
$_SESSION[foo] = 66;
?>
Session 的讀取:
範例碼:
<?php // 1. 啟動 Session session_start(); // 2. 使用$_SESSION 全域變數,讀取 Session 變數。 echo $_SESSION[foo]; ?>
Session 的清除:
範例碼:
<?php // 1. 啟動 Session session_start(); // 2. 清除所有已登記的 Session 變數 session_unset(); // 3. 銷毀現有的 Session連線紀錄 session_destroy(); ?>
實例演練:
以下使用簡單的帳號登入,來示範 Session 的使用。在這個簡化的演練中,不使用資料庫,僅著重在於 Session 的應用,以加深學習的效果。在這個範例中,有以下幾個網頁:
一、靜態網頁:index.htm, access_denied.htm
二、login.php:
原始碼:
<?php
if(isset($_POST[Submit])){
// 如果帳號和密碼正確的話,寫入Session變數,並視情況重導到相關的頁面
if($_POST[account] == "admin" && $_POST[password] == "pass"){
// 啟動 Session
session_start();
// 登記 Session 變數名稱
session_register('authenticated');
session_register('fruits');
session_register('login_time');
// 寫入 Session 變數值
$_SESSION['authenticated'] = true;
$_SESSION['fruits'] = array("orange", "banana", "apple");
$_SESSION['login_time'] = date('Y-m-d h:i:s');
// 檢查在 $_SESSION 全域變數中,是否有之前設定好的網址重導 Session 變數
if(isset($_SESSION[UrlRedirect])){
$redir = $_SESSION[UrlRedirect]
}else{
$redir = 'login_success.php';
}
// 重導到相關頁面
header("Location: $redir");
exit;
}else{
header('Location: access_denied.htm');
exit;
}
}
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
</head>
<body>
<form name="form1" id="form1" method="post"
action="">
<p align="center">帳號:
<input name="account" type="text" id="account"
/>
</p>
<p align="center">密碼:
<input name="password" type="password" id="password"
/>
</p>
<p align="center">
<input type="submit" name="Submit" value="登 入"
/>
</p>
</form>
</body>
</html>
三、login_success.php:
原始碼:
<?php session_start();
if($_SESSION[authenticated] == true){
$result = '登入時間: '.$_SESSION[login_time];
foreach($_SESSION[fruits] as $fruit){
$result .= "<br>$fruit";
}
}else{
header('Location: login.php');
exit;
} ?> <html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
</head>
<body>
<div align="center">
<p><font color="#0000FF"><strong>登入成功!</strong></font></p>
<p>Session 變數如下:</p>
<p><?php echo $result?></p>
<p><a href="logout.php">[ 登 出 ]</a></p>
</div>
</body>
</html>
四、protected.php:
原始碼:
<?php session_start();
session_register('UrlRedirect');
$_SESSION[UrlRedirect] = $PHP_SELF;
if($_SESSION[authenticated] == true){
$result = '登入時間: '.$_SESSION[login_time];
foreach($_SESSION[fruits] as $fruit){
$result .= "<br>$fruit";
}
}else{
header('Location: login.php');
exit;
} ?> <html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
</head>
<body>
<div align="center">
<p><font color="#0000FF"><strong>權限控管頁面!</strong></font></p>
<p>Session 變數如下:</p>
<p><?php echo $result?></p>
<p><a href="logout.php">[ 登 出 ]</a></p>
</div>
</body>
</html>
五、logout.php:
原始碼:
<?php session_start();
session_unset();
session_destroy();
header('Location: index.htm');
exit; ?>