ปรึกษาเรื่อง session ครับ

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
aei_ou
PHP Full Member
PHP Full Member
โพสต์: 39
ลงทะเบียนเมื่อ: 22/01/2009 2:53 pm

ปรึกษาเรื่อง session ครับ

โพสต์ที่ยังไม่ได้อ่าน โดย aei_ou »

คือผมจะทำ session โดยกำหนด Level ให้กับ User แต่ละคน โดยแบ่ง Level เป็น 5 ระดับ ได้แก่ 0 - 4

โดยที่แต่ละLevel จะมีสิทธิ์เข้าใช้งานระบบที่แตกต่างกันไป แล้วผมต้องการให้ User ที่จะเข้าใช้งาน Login เข้ามา

สมมุติว่า Username1 ระดับของ Level คือ 0 ต้องการให้ล็อคอินเข้ามา แล้ว Redirect ไปที่ หน้า level0.php เลยครับ

ส่วน Username2 ระดับของ Level คือ 1 ให Redirect ไปที่ หน้า level1.php ประมาณนี้ครับ

อันนี้ Code หน้า Login.php

<?php

session_start();

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($accesscheck)) {
$GLOBALS['PrevUrl'] = $accesscheck;
session_register('PrevUrl');
}

if (isset($_POST['UserLogin'])) {
$loginUsername=$_POST['UserLogin'];
$password=$_POST['UserPwd'];
$MM_fldUserAuthorization = "UserPwd";
$MM_redirectLoginSuccess = "level0.php";
$MM_redirectLoginFailed = "login_fail.php";
$MM_redirecttoReferrer = true;
mysql_select_db($database_connect_eoffice, $connect_eoffice);

$LoginRS__query=sprintf("SELECT UserLogin, UserPwd FROM User WHERE UserLogin='%s' AND UserPwd='%s'",
get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));

$LoginRS = mysql_query($LoginRS__query, $connect_eoffice) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {

$loginStrGroup = mysql_result($LoginRS,0,'UserPwd');

$GLOBALS['MM_Username'] = $loginUsername;
$GLOBALS['MM_UserGroup'] = $loginStrGroup;

session_register("MM_Username");
session_register("MM_UserGroup");

if (isset($_SESSION['PrevUrl']) && true) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>
/////////////////////////////////////////////////////////////////////////////////

หน้า Level0.php (หน้านี้ผมให้มันเช็ค Password จากหน้า Login ถ้า Pass เป็น1234 ถึงจะเข้าได้)

<?php

session_start();

$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){

session_unregister('MM_Username');
session_unregister('MM_UserGroup');

$logoutGoTo = "index.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}
?>
<?php
session_start();
$MM_authorizedUsers = "1234";
$MM_donotCheckaccess = "false";

function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {

$isValid = False;

if (!empty($UserName)) {

$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}

if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && false) {
$isValid = true;
}
}
return $isValid;
}

$MM_restrictGoTo = "login_fail.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
$MM_referrer .= "?" . $QUERY_STRING;
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>

ผมจะต้องแก้หรือปรับเปลี่ยนยังไงบ้างคับ แนะนำหน่อย
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41379
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ปรึกษาเรื่อง session ครับ

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ก่อนอื่นที่จะแก้ นะครับ ตอนนี้ มีปัญหาอะไรครับ :?:
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
aei_ou
PHP Full Member
PHP Full Member
โพสต์: 39
ลงทะเบียนเมื่อ: 22/01/2009 2:53 pm

Re: ปรึกษาเรื่อง session ครับ

โพสต์ที่ยังไม่ได้อ่าน โดย aei_ou »

ปัญหาคือ ไม่รู้จะเขียนโค้ดยังไงให้มันเช็ค Level แล้ว Redirect ไปหน้าที่กำหนด

เช่น level0 เช็คแล้วให้ไปหน้า level0.php
เช่น level1 เช็คแล้วให้ไปหน้า level1.php
เช่น level2 เช็คแล้วให้ไปหน้า level2.php
เช่น level3 เช็คแล้วให้ไปหน้า level3.php
เช่น level4 เช็คแล้วให้ไปหน้า level4.php

ยังงงๆมึนๆอยู่

ปกติผมใช้ Dream เขียนครับ แบบนี้ http://school.obec.go.th/prakr/homework ... chap17.htm
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41379
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ปรึกษาเรื่อง session ครับ

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

$MM_redirectLoginSuccess = "level0.php"; <--------------- ใส่เงือนไขที่ต้องการ ลงไป ครับ
เช่น
if($level==1) {
$MM_redirectLoginSuccess = "level1.php";
}
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
aei_ou
PHP Full Member
PHP Full Member
โพสต์: 39
ลงทะเบียนเมื่อ: 22/01/2009 2:53 pm

Re: ปรึกษาเรื่อง session ครับ

โพสต์ที่ยังไม่ได้อ่าน โดย aei_ou »

ขอบคุณครับบบบ เดี๋ยวลองทำดูครับ

ถ้าติดอะไรอีกจะมารบกวนใหม่ครับ

แต๊งกิ้ว
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: Google [Bot], Google Adsense [Bot] และบุคลทั่วไป 140