課程115:使用 SESSION 來記錄 Web 應用程式的狀態


摘要:

HTTP 使用無連線狀態的連線(Stateless Connection)方式,然而,我們在開發應用程式時,常會需要在多個網頁,存取同一位使用者連線資料。PHP解決這個問題的方式,有以下幾種:

  1. Cookie
  2. Session
  3. Hidden Field 隱藏欄位。
  4. URL Rewriting 網址改寫。

在這個課程中,我們著重在討論 Session的使用,並把 Session 應用在系統登入的實例中。


Session 變數的使用:

PHP 的全域變數 Session 提供給我們一個可以在多個頁面存取個別使用者連線資料的機制。對於針對個別使用者,需要在多個頁面存取共用的資料時,特別有用。如果,非由程式特別清除 Session 的話,個別使用者的 Session 資料會在特定時間(由 php.ini 設定)內,被系統清除掉。

以下,我們分別按照寫入Session、讀取Session、清除Session等方式,探討如何使用 Session 變數。


Session 的寫入:

  1. 啟動 Session。
  2. 登記 Session 變數名稱。
  3. 使用$_SESSION 全域變數,將值寫入已登記的 Session 變數。

範例碼:

<?php
	// 1. 啟動 Session
	session_start();


	// 2. 登記 Session 變數名稱
	session_register('foo');


	// 3. 寫入 Session 變數
	$_SESSION[foo] = 66;

?>

Session 的讀取:

  1. 啟動 Session。
  2. 使用$_SESSION 全域變數,讀取 Session 變數。

範例碼:

<?php
	// 1. 啟動 Session
	session_start();


	// 2. 使用$_SESSION 全域變數,讀取 Session 變數。
	echo $_SESSION[foo];

?>

Session 的清除:

  1. 啟動 Session。
  2. 清除所有已登記的 Session 變數。
  3. 銷毀現有的 Session連線紀錄。

範例碼:

<?php
	// 1. 啟動 Session
	session_start();


	// 2. 清除所有已登記的 Session 變數
	session_unset();

	// 3. 銷毀現有的 Session連線紀錄
	session_destroy();
?>

實例演練:

以下使用簡單的帳號登入,來示範 Session 的使用。在這個簡化的演練中,不使用資料庫,僅著重在於 Session 的應用,以加深學習的效果。在這個範例中,有以下幾個網頁:

  1. index.htm 主頁:包含系統登入,以及權限控管頁面的連結。
  2. login.php 登入頁:負責檢查帳號密碼是否正確,登入成功的話,將變數寫入 Session,供其他權限控管頁面存取;並按照使用者在主頁所點選的連結,決定重導的網址。登入失敗時,則重導到登入失敗的頁面。
  3. access_denied.htm 登入失敗頁:顯示登入失敗的訊息。
  4. login_success.php 登入成功頁:如果,使用者在主頁所點選的連結為系統登入,且登入成功時,則傳回這個網頁,並讀取 Session 中的變數值,顯示在這個頁面。
  5. protected.php 權限控管頁:如果,使用者在主頁所點選的連結為權限控管頁面,且登入成功時,則傳回這個網頁,並讀取 Session 中的變數值,顯示在這個頁面。
  6. logout.php 登出頁:清除所有 Session 變數,銷毀現有連線資料,並重導回主頁。



實作:

※ 網頁製作完成後,請將 115 資料夾上傳至網站的目錄※

一、靜態網頁:index.htm, access_denied.htm

  1. 在你的電腦上,作一個資料夾 115 。
  2. 用 Dreamweaver MX 或其他任何網頁編輯工具,製作這兩個檔案。
  3. 請點選以下兩個連結,參考這兩個網頁的畫面:

二、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; ?>