Transcript Session

PHP session / Login
Professional Home Page :PHP
เกียรติพงษ์ ยอดเยีย่ มแกร
Session Control
 ระบบการติดตามผูใ้ ช้ ให้ติดต่อกับตัวให้บริ การเว็บด้วยการเชื่อมต่อเพียง
หนึ่งเดียว
 การทางานจะสนับสนุนด้วย Session ID : ซึ่งเป็ นเลขสุ่ มสร้างโดย PHP
เก็บไว้ที่ Server และเครื่ องผูใ้ ช้ และจะคงอยูต่ ามอายุการใช้งาน
Session
 session_start();
 ฟังก์ ชั่นเริ่มเปิ ดการทางาน Session
 session_register(“ ตัวแปร Session “);
 ฟังก์ ชั่นจัดเก็บค่ า Session ซึ่งมีผลให้ PHP สุ่ มค่ าการจัดเก็บ
 เช่ น
session_register(“valid_user”);  นาค่ าตัวแปร valid_user เก็บไว้ ใน session variable
พร้ อมกับ session id
 session_is_registered(“ตัวแปร Session”);
 ฟังก์ ชั่นตรวจสอบว่ า ตัวแปร Session นั้นได้ ทาการ register ไปหรือยัง
 เช่ น
session_is_registered(“valid_user”);  ผลที่ได้ คอื True หรือ False
Session
 session_unregister(“ ตัวแปร Session “);
 ฟังก์ชนั่ สาหรับยกเลิกการจัดเก็บและใช้งานตัวแปร Session
 เช่น
session_unregiste(“valid_user”);
 session_destroy();
 ตัดการเชื่อมต่อด้วย session
 unset(“ตัวแปร”);
 ล้างค่าตัวแปร ยกเลิกการใช้ตวั แปรออกจากหน่วยความจา
Session
 $HTTP_SESSION_VARS[“ตัวแปร Session”]
 อ่านค่าจากตัวแปร Session
 เช่น $user = $HTTP_SESSION_VARS[“valid_user”];
 session_unset();
 ยกเลิกการใช้งาน session ทั้งหมด ตัวแปร session ทุกตัวจะถูกยกเลิก
กรณีศึกษา Login .. Logout
<form name="form1" method="post" action="login_process.php">
<table width="500" border="1" cellpadding="0" cellspacing="1" bordercolor="#333333">
<tr>
<td bordercolor="#FFFFFF"><table width="500" border="0" cellspacing="1" cellpadding="3">
<tr align="right" bgcolor="#00CCFF">
<td colspan="2">Login เข ้าสูร่ ะบบ</td>
</tr>
<tr>
<td width="160">User Name /td>
<td width="337"><input name="user" type="text" id="user2"></td>
</tr>
<tr>
<td>Password/td>
<td><input name="password" type="password" id="password2"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table></td>
</tr>
</table>
</form>
Create include
ื่ staff.php
File ชอ
<?
$adminuser = ‘ADMIN’;
$adminpass = ‘123456’;
?>
Sessoin register
<?
?>
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter("must-revalidate");
session_start();
include("staff.inc.php");
$user = $HTTP_POST_VARS["user"];
$pass = $HTTP_POST_VARS["password"];
$valid_user = $user;
if (strtoupper($user) == strtoupper($adminuser))
{
if ($adminpass == $pass)
{
session_register("valid_user");
header("Location:pay.php");
}
else
{
header("Location:login.html");
}
}
else
{
header("Location:login.html");
}
ตรวจสถานะ Session
admin.php
<?
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter("must-revalidate");
ob_start();
session_start();
//fix to help internet explorer remember form variables
header("Cache-control: private"); //IE 6 fix
if (session_is_registered("valid_user"))
{
$valid_user = $HTTP_SESSION_VARS[“valid_user”];
echo “<h2>Hello welcome admin ..</h2><br>”;
echo “<a href=‘logout.php?valid_user=$valid_user’> Logout </a>”;
}
else
{
header("Location:login.html");
}
?>
logout
logout.php
<?
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter("must-revalidate");
ob_start();
session_start();
$valid_user = $HTTP_GET_VARS[“valid_user"];
if (session_is_registered("valid_user"))
{
$valid_user = $HTTP_SESSION_VARS["valid_user"];
$result = session_unregister("valid_user");
session_destroy();
unset($valid_user);
header("location:login.php");
}
?>