พบกับบทความเกี่ยวกับคอมพิวเตอร์และ programming ได้ที่ http://articles.modoeye.com | บล็อกอาหาร
PHP development / ถามเกี่ยวกับการทำ login ครับ
KB
KB
Sun 13 Jan 2008 23:25:55

คือผมใช้ dreamweaver สร้างการตรวจสอบผู้ใช้ผ่าน login ให้อัตโนมัตินะครับ ทำให้ตัวแปรสำหรับ text feild ที่อยู่ใน from ไม่ส่งค่าไปอีกหน้าอะครับ ผมต้องการให้มันส่งค่า user ไปอีกหน้าอะครับ ไม่ทราบว่าต้องแก้โคด้อย่างไร ผมจะแสดงโค้ดให้ดูนะครับ

<?php require_once('../Connections/dbconn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

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

if (isset($_POST['user'])) {
  $loginUsername=$_POST['user'];
  $password=$_POST['pass'];
  $MM_fldUserAuthorization = "level";
  $MM_redirectLoginSuccess = "../DaySaleReport.php";
  $MM_redirectLoginFailed = "login.php";
  $MM_redirecttoReferrer = true;
  mysql_select_db($database_dbconn, $dbconn);
   
  $LoginRS__query=sprintf("SELECT `user`, pass, level FROM member WHERE `user`=%s AND pass=%s",
  GetSQLValueString($loginUsername, "-1"), GetSQLValueString($password, "text"));
  
  $LoginRS = mysql_query($LoginRS__query, $dbconn) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
   
    $loginStrGroup  = mysql_result($LoginRS,0,'level');
   
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;      

    if (isset($_SESSION['PrevUrl']) && true) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl']; 
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>Untitled Document</title>
</head>

<body>
<form ACTION="<?php echo $loginFormAction; ?>" method="POST" name="form1" target="_parent" id="form1">
Username
<label>
<input type="text" name="user" id="user" />
</label>
  <p>Password 
    <label>
    <input type="password" name="pass" id="pass" />
    </label>
    <input name="hiddenField" type="hidden" id="hiddenField" value="<? echo $user; ?>" />
  </p>
  <p>
    <label>
    <input type="submit" name="login" id="login" value="login" />
    </label>
</p>
</form>

</body>
</html>

ข้างบนๆ คือส่วนที่โปรแกรมันสร้างให้นะครับ

KB
KB
Sun 13 Jan 2008 23:59:49

ผมแก้ได้แล้วครับ ผมไปใช้เครื่องมือ Restrict Access To Page แล้วก็ไปหายตัวแปร ก็เจอว่า  ตัวแปรนี้มันบอก $_SESSION['MM_Username'] นี่โค้ดครับ

<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "admin,saler";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
  // For security, start by assuming the visitor is NOT authorized.
  $isValid = False;

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
  // Therefore, we know that a user is NOT logged in if that Session variable is blank.
  if (!empty($UserName)) {
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
    // Parse the strings into arrays.
    $arrUsers = Explode(",", $strUsers);
    $arrGroups = Explode(",", $strGroups);
    if (in_array($UserName, $arrUsers)) {
      $isValid = true;
    }
    // Or, you may restrict access to only certain users based on their username.
    if (in_array($UserGroup, $arrGroups)) {
      $isValid = true;
    }
    if (($strUsers == "") && false) {
      $isValid = true;
    }
  }
  return $isValid;
}

$MM_restrictGoTo = "Sample2/login.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;
}
?>

เพื่อคนอื่นจะศึกษา ต่อไปผมจะเอาข้อมูลออกมาแสดงโดยอ้างอิงตัวแปล $_SESSION['MM_Username'] ถ้าเจอปัญหาแล้วจะมาถามอีกนะครับ

Reply
Name:
E-mail:
Home | Services | Forum | Classified | Directories | Support | Contact
ATOM feed RSS 0.9 feed RSS 1.0 feed RSS 2.0 feed
Copyright © 2005 - 2007 Modoeye.com, All Rights Reserved.
Disclaimer | Privacy policy | Term of Use | Term of Services
Valid XHTML Valid CSS! PHP: Hypertext Preprocessor MySQL database Apache Powered! FreeBSD Power to serve
Modoeye Sitemap Client login