1 / 10

PHP session / Login Professional Home Page : PHP

PHP session / Login Professional Home Page : PHP. เกียรติพงษ์ ยอดเยี่ยมแกร. Session Control. ระบบการติดตามผู้ใช้ ให้ติดต่อกับตัวให้บริการเว็บด้วยการเชื่อมต่อเพียงหนึ่งเดียว

isi
Download Presentation

PHP session / Login Professional Home Page : PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP session / LoginProfessional Home Page :PHP เกียรติพงษ์ ยอดเยี่ยมแกร

  2. Session Control • ระบบการติดตามผู้ใช้ ให้ติดต่อกับตัวให้บริการเว็บด้วยการเชื่อมต่อเพียงหนึ่งเดียว • การทำงานจะสนับสนุนด้วย Session ID : ซึ่งเป็นเลขสุ่มสร้างโดย PHP เก็บไว้ที่ Server และเครื่องผู้ใช้ และจะคงอยู่ตามอายุการใช้งาน

  3. 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

  4. Session • session_unregister(“ตัวแปร Session “); • ฟังก์ชั่นสำหรับยกเลิกการจัดเก็บและใช้งานตัวแปร Session • เช่น session_unregiste(“valid_user”); • session_destroy(); • ตัดการเชื่อมต่อด้วย session • unset(“ตัวแปร”); • ล้างค่าตัวแปร ยกเลิกการใช้ตัวแปรออกจากหน่วยความจำ

  5. Session • $HTTP_SESSION_VARS[“ตัวแปร Session”] • อ่านค่าจากตัวแปร Session • เช่น $user = $HTTP_SESSION_VARS[“valid_user”]; • session_unset(); • ยกเลิกการใช้งาน session ทั้งหมด ตัวแปร session ทุกตัวจะถูกยกเลิก

  6. กรณีศึกษา 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>

  7. Create include File ชื่อ staff.php <? $adminuser = ‘ADMIN’; $adminpass = ‘123456’; ?>

  8. Sessoin register <? header("Expires: Sat, 01 Jan 200000: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"); } ?>

  9. ตรวจสถานะ Session admin.php <? header("Expires: Sat, 01 Jan 200000: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"); } ?>

  10. logout logout.php <? header("Expires: Sat, 01 Jan 200000: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"); } ?>

More Related