ขอถามอะไรหน่อยค่ะเกี่ยวกับ PHP :: เมื่อผู้ใช้ login เข้าระบบแล้ว ถ้าเราจะดักจับว่า รหัสนักศึกษา 5210210xxx เข้าหน้าเว็บหน้านึง แต่ถ้ารหัสนักศึกษา 5310210xxx ก็ให้เข้าอีกหน้า และถ้ารหัสนักศึกษา 5410210xxx ก็เข้าอีกหน้านึง ทำไงดีค่ะ ช่วยหน่อยค่ะ TT'
ใช้ if มาช่วยครับ ถ้าจะให้ดีขอข้อมูลละเอียดกว่านี้ครับ
Date :
2012-07-19 21:17:43
By :
softwarelp
form_login.php
<strong><strong><html>
<head>
<title>Index</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<!-- ImageReady Slices (Index.psd) -->
<table id="Table_01" width="1024" height="575" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td width="1024" height="29">
<p> </p></td>
</tr>
<tr>
<td height="283" colspan="10">
<img src="images/Index_09.png" width="1024" height="283" alt=""></td>
</tr>
<tr>
<td height="19" colspan="10">
<form name="form1" method="post" action="check.php" >
<img src="images/Index_11.png" width="318" height="37" alt=""><img src="images/Index_12.png" width="82" height="37" alt=""><img src="images/Index_16.png" width="308" height="37" alt=""><br>
<center>Login</center><br>
<center><table border="1" style="width: 300px">
<tbody>
<tr>
<td> Username</td>
<td>
<input name="txtusername" type="text" id="txtUsername" align="center"> </td>
</tr>
<tr>
<td> Password</td>
<td><input name="txtpassword" type="password" id="txtPassword"> </td>
</tr>
</tbody>
</table>
<p><a href="register.php">สมัครสมาชิก</a><br>
</p>
</center>
<center><input type="submit" name="Submit" value="Login"><br><br>
</center>
</form></td>
</tr>
<tr>
<td height="32" colspan="10">
<img src="images/Index_17.png" width="1024" height="44" alt=""></td>
</tr>
</table>
<p>
<!-- End ImageReady Slices -->
</p>
<p> </p>
</body>
</html></strong></strong>
check.php
<?
session_start();
$hostname = "localhost"; //ชื่อโฮสต์
$user = "root"; //ชื่อผู้ใช้
$password = "1234"; //รหัสผ่าน
$dbname = "test1"; //ชื่อฐานข้อมูล
$tblname = "member"; //ชื่อตาราง
// เริ่มติดต่อฐานข้อมูล
mysql_connect($hostname, $user, $password) or die("ติดต่อฐานข้อมูลไม่ได้");
// เลือกฐานข้อมูล
mysql_select_db($dbname) or die("เลือกฐานข้อมูลไม่ได้");
// คำสั่ง SQL และสั่งให้ทำงาน
$sql = "select * from $tblname where username='$txtusername' and password='$txtpassword'"; //เช็คค่าข้อมูลที่ส่งมาจากฟอร์ม
$objQuery = mysql_query($sql);
$objResult = mysql_fetch_array($objQuery);
if(!$objResult)
{
echo "Username and Password Incorrect!";
}
else
{
$_SESSION["UserID"] = $objResult["UserID"];
$_SESSION["Status"] = $objResult["Status"];
session_write_close();
if($objResult["Status"] == "ADMIN")
{
header("location:admin.php");
}
else
{
header("location:index2.php");
}
}
mysql_close();
?>
ตรงไฟล์ check.php นี้อ่ะค่ะ
คือหนูดักแค่ถ้าเป็น ADMIN ก็เข้าหน้าเว๊บอีกหน้า และถ้าเป็น USER ก้เข้าอีกหน้า
แต่คนที่เป็น USER หนูต้องการดักอีกว่า ถ้ารหัส 5210210xxx เข้ามา ก้เข้าไปอีกหน้า
ถ้ารหัส 5310210xxx เข้ามาก้ไปอีกหน้าอ่ะค่ะ
** พอจะเข้าใจมั้ยค่ะ TT'
Date :
2012-07-19 21:25:21
By :
เด็กน้อย
ช่วยตอบหนูหน่อยน่ะค่ะ TT"
Date :
2012-07-19 21:50:46
By :
เด็กน้อย
โค้ดข้างบน มันก็เข้าของใครของมันอยู่แล้วนะ ใช้ session อยู่แล้วนี่ user ก็คือรหัสประจำตัว พาส ของใครของมัน มันจะเด้่งไปหน้า index2 หน้า index2 ต้องเปิด session ด้วยนะ แต่ถ้าอยากให้เด้งไปหน้าอื่น หรือหน้าแสดงข้อมูลคนเข้าระบบ ก็ไปสร้างโค้ดดึงมาจากฐานข้อมูลซิ
คำว่าต้องการให้เด้งไปคนละหน้านี่ คงไม่ได้หมายความว่า เด้งไปหน้า ทุกรหัส แล้วต้องสร้างไฟล์ทุกรหัสใช่ไหม *-*
ประวัติการแก้ไข 2012-07-19 22:26:03 2012-07-19 22:28:21
Date :
2012-07-19 22:22:53
By :
นู๋น้อย
$student_id = substr("ตรงนี้เอารหัสนักศึกษามาใส่", 0, 7)
if ($student_id==5210210)
{
header ("Location: หน้าที่ต้องการ");
exit(0);
}
elseif ($student_id==5310210)
{
header ("Location: หน้าที่ต้องการ");
exit(0);
}
elseif ($student_id==5410210)
{
header ("Location: หน้าที่ต้องการ");
exit(0);
}
Date :
2012-07-19 22:30:06
By :
adaaugusta
ใช่ค่ะ ๆๆ .. เข้าคนล่ะหน้าอยุ่แล้ว แต่ USER หนู มีทั้งหมด (5210210xxx,5310210xxx,5410210xxx,5510210xxx) ซึ่งแต่ล่ะรหัสจะเข้าหน้าเว็บไม่เหมือนกันค่ะ
**** คำว่าต้องการให้เด้งไปคนละหน้านี่ คงไม่ได้หมายความว่า เด้งไปหน้า ทุกรหัส แล้วต้องสร้างไฟล์ทุกรหัสใช่ไหม *-* << แบบนี้เลยค่ะ 55
Date :
2012-07-19 22:34:50
By :
เด็กน้อย
แบบพี่ กะล่อนเลยค่ะ ตกใจนึกว่าจะทำรหัสทั้งมหาลัย *-*
Date :
2012-07-19 22:38:50
By :
นู๋น้อย
$student_id = substr("ตรงนี้เอารหัสนักศึกษามาใส่", 0, 7)
รหัสนักศึกษานี้คือแบบไหนอ่ะค่ะ งง ?? แหะ ๆๆ ^^
Date :
2012-07-19 22:45:59
By :
เด็กน้อย
substr("ตรงนี้เอารหัสนักศึกษามาใส่", 0, 7)
คือ ตัวตัดคำจร้า
Date :
2012-07-19 22:48:27
By :
นู๋น้อย
ตรงนี้เอารหัสนักศึกษามาใส่ <<< แล้วรหัสนี้คือรหัสไหนค่ะ รหัสนักศึกษาทั้งหมดหรือค่ะ ??
Date :
2012-07-19 22:50:23
By :
เด็กน้อย
น่าจะใช่ค่ะ
Date :
2012-07-19 22:51:45
By :
นู๋น้อย
ก็รหัสนักศึกษาแบบเต็มไง
เราเอามาจากไหนล่ะ ถ้าเอามาจาก DB ก็ใส่ไปเลย
substr($record['student_id'] , 0, 7);
ถ้าเอามาจากฟอร์ม ก็ input มา
substr($_POST['student_id'] , 0, 7);
ถ้า Get มาจาก Url ก็
substr($_GET['student_id'] , 0, 7);
ปล. [student_id] เป็นชื่อตัวแปรที่เราส่งมานะจ๊ะ นี้แค่ยกตัวอย่าง
ปล.2 เอาเฉพาะคนที่ต้องการสิ เช่นใครล็อกอินเข้ามาก็ให้เอา รหัสนักศึกษาคนนั้นมาใส่
ประวัติการแก้ไข 2012-07-19 22:54:46
Date :
2012-07-19 22:53:14
By :
adaaugusta
$student_id = substr("5210210,5310210,5410210", 0, 7)
if ($student_id==5210210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($student_id==5310210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($student_id==5410210)
{
header ("Location: index3.php");
exit(0);
แบบนี้รึป่าวค่ะ งงมากเลยค่ะ TT'
Date :
2012-07-19 22:55:00
By :
เด็กน้อย
กำ อิอิ ลืมไปว่าทำไฟล์ check หุหุ ลืม....
Date :
2012-07-19 22:57:15
By :
นู๋น้อย
ถ้าเป็นแบบนี้ มันฟ้อง error บรรทัด if ($student_id==5210210) ค่ะ
Date :
2012-07-19 22:57:34
By :
เด็กน้อย
$student_id = substr("5210210,5310210,5410210", 0, 7)
ต้องรับค่าจร้า แบบพี่กะล่อนเลย รับค่ามาจากฟอร์ม ช่องเข้าระบบ
ประวัติการแก้ไข 2012-07-19 23:01:37 2012-07-19 23:02:21
Date :
2012-07-19 22:58:59
By :
นู๋น้อย
เอามาจาก DB ค่ะ !!! .. แต่มันฟ้อง error ว่า Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\AppServ\www\check.php on line 53
line 53 คือ $UserID = substr("$record['UserID']", 0, 7) ค่ะ TT
$UserID = substr("$record['UserID']", 0, 7)
if ($UserID==5210210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($UserID==5310210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($UserID==5410210)
{
header ("Location: index3.php");
exit(0);
Date :
2012-07-19 23:04:07
By :
เด็กน้อย
ผิดๆๆๆๆ อย่างงสิ
ไฟล์นี้แค่เป็นตัวบอกว่า รหัสนักศึกษา 7 หลักแรกขึ้นต้นด้วยอะไรต้องไปที่หน้าไหน
เราก็ต้องหาวิธีเอาสิว่า คนที่เข้ามาใช้บริการระบบของเราน่ะ เค้ารหัสนักศึกษาอะไร
เช่น มี นาย A, นาย B , นาย C
นาย A ล็อกอินเข้ามา ให้เก็บ รหัสนักศึกษาเอาไว้แล้วเอาไปใส่ตามโค้ดที่ได้ให้ไปแล้วนั้น แล้วไปตามหน้าที่กำหนดไว้
อีกสองคนที่เหลือก็ทำเหมือนกัน
Date :
2012-07-19 23:04:43
By :
adaaugusta
$UserID = substr($UserID, 0, 7)
เอะ ว่าแต่..UserID มันลำดับมะใช่หรอ มันจะเช็คได้ไงอ่ะ
ลองแบบนี้ซิได้ม่ะ เราก็ม่ะค่อยเก่งน้า แต่อยากช่วย อิอิ
ประวัติการแก้ไข 2012-07-19 23:06:45 2012-07-19 23:08:47
Date :
2012-07-19 23:05:47
By :
นู๋น้อย
$UserID = substr("$record['UserID']", 0, 7) เปลี่ยนเป็น
$UserID = substr($record['UserID'] , 0, 7);
ปล. $record คือ ค่า Array ของ นักศึกษาที่ได้จากการ Select ตอน Query มาใช้ตัวแปรอะไรให้เปลี่ยนด้วย อย่าลอกตาม
Date :
2012-07-19 23:06:22
By :
adaaugusta
มันก้ยัง error ค่ะ Parse error: syntax error, unexpected T_IF in C:\AppServ\www\check.php on line 55
line 55 คือ if ($txtUsername==5210210)
$UserID = substr($txtUsername['UserID'], 0, 7)
if ($txtUsername==5210210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($txtUsername==5310210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($txtUsername==5410210)
{
header ("Location: index3.php");
exit(0);
Date :
2012-07-19 23:13:29
By :
เด็กน้อย
ขอโทษน่ะค่ะที่ถามเยอะไปหน่อย พอดีทำโปรเจคอยุ่ค่ะ TwT'
Date :
2012-07-19 23:19:04
By :
เด็กน้อย
ขอโทดนะคะ รหัสมันอยู่ ฟิล ชื่อไรคะ
ประวัติการแก้ไข 2012-07-19 23:21:30 2012-07-19 23:28:31
Date :
2012-07-19 23:20:45
By :
นู๋น้อย
$_SESSION["UserID"] = $objResult["UserID"];
$_SESSION["Status"] = $objResult["Status"];
session_write_close();
if($objResult["Status"] == "ADMIN")
{
header("location:admin.php");
}
else
{
header("location:index2.php");
}
ตอนแรกหนูให้ ADMIN กับ USER เข้าคนล่ะลิ้งกัน
แต่พอเพิ่มคำสั่ง นี้ไป รหัส 5210210 ยังเข้า index2.php อยุ่เลยค่ะ
หรือว่าหนูใส่ผิดที่ค่ะ
$UserID = substr($txtUsername['UserID'], 0, 7);
if ($UserID==5210210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($UserID==5310210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($UserID==5410210)
{
header ("Location: index3.php");
exit(0);
}
Date :
2012-07-19 23:24:28
By :
เด็กน้อย
เอาชุดเช็คเงื่อนไขรหัสนักศึกษา ไปไว้ก่อน
if($objResult["Status"] == "ADMIN")
{
สิ
ไปไว้ทีหลังมันก็ทำ
if($objResult["Status"] == "ADMIN")
{
ก่อน
Date :
2012-07-19 23:31:47
By :
adaaugusta
มันติดif Statusอยู่ค่ะ มันเช็คไล่ลงมา มันติดข้างบนค่ะ เอาออกค่ะ หรือเอาไว้ท้ายค่ะ
ประวัติการแก้ไข 2012-07-19 23:33:32
Date :
2012-07-19 23:33:02
By :
นู๋น้อย
$_SESSION["UserID"] = $objResult["UserID"];
$_SESSION["Status"] = $objResult["Status"];
$UserID = substr($txtUsername['UserID'], 0, 7);
if ($UserID==5210210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($UserID==5310210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($UserID==5410210)
{
header ("Location: index3.php");
exit(0);
}
session_write_close();
if($objResult["Status"] == "ADMIN")
{
header("location:admin.php");
}
else
{
header("location:index2.php");
}
แบบนี้รึป่าวค่ะ มันก้ยังขึ้นหน้า index2.php อยุ่อ่ะค่ะ
Date :
2012-07-19 23:35:01
By :
เด็กน้อย
ใส่
echo 'ค่าของ $UserID คือ : '.$UserID;
ต่อจากบรรทัด
$UserID = substr($txtUsername['UserID'], 0, 7);
ดูว่าออกอะไรมา
Date :
2012-07-19 23:55:21
By :
adaaugusta
ถ้ายังไม่ได้ เดี๋ยวพรุ่งนี้มาลองทำดูค่ะ นู๋ไปนอนก่อนน้าคร้า.....*-* พี่กะล่อน สู้ๆ
Date :
2012-07-20 00:10:19
By :
นู๋น้อย
$_SESSION["UserID"] = $objResult["UserID"];
$_SESSION["Status"] = $objResult["Status"];
$UserID = substr($txtUsername['UserID'], 0, 7);
echo '5210210660 : '.$UserID;
if ($UserID==5210210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($UserID==5310210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($UserID==5410210)
{
header ("Location: index3.php");
exit(0);
}
session_write_close();
if($objResult["Status"] == "ADMIN")
{
header("location:admin.php");
}
else
{
header("location:index2.php");
}
แบบนี้รึป่าวค่ะ มันก้ยัง error ::: 5210210660 :
Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\check.php:45) in C:\AppServ\www\check.php on line 71
71 คือ header("location:index2.php");
Date :
2012-07-20 00:17:27
By :
เด็กน้อย
$txtUsername = substr($txtUsername['Username'], 0, 7);
echo 'ค่าของ $Username คือ : '.$Username;
ขึ้นแล้วค่ะ เป็น รหัสนักศึกษา 5210210001
แต่พอใส่
if ($Username==5210210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($Username==5310210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($Username==5410210)
{
header ("Location: index3.php");
exit(0);
}
มันไม่ทำงานค่ะ..
Date :
2012-07-20 00:31:17
By :
เด็กน้อย
$_SESSION["Username"] = $objResult["Username"];
$_SESSION["Status"] = $objResult["Status"];
$txtUsername = substr($txtUsername['Username'], 0, 7);
/*echo 'ค่าของ $Username คือ : '.$Username;*/
if ($txtUsername==5210210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($txtUsername==5310210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($txtUsername==5410210)
{
header ("Location: index3.php");
exit(0);
}
แบบนี้ก้ไม่ทำงานค่ะ ขึ้นหน้าว่างเลย TT'
Date :
2012-07-20 00:33:09
By :
เด็กน้อย
Code (PHP)
$code = "5210210xxx";
//$code = $_REQUEST['studentID']; #ถ้ารับค่ามาจากฟอร์ม
//$code = $_SESSION["UserID"]; #รับค่าจาก Session
$inum=substr($code, 0, 7);
switch ($inum) {
case "5210210":
echo "5210210";
header ("Location: หน้าที่ต้องการ");
break;
case "5310210":
echo "5210210";
header ("Location: หน้าที่ต้องการ");
break;
case "5410210":
echo "5210210";
header ("Location: หน้าที่ต้องการ");
case "5510210":
echo "5510210";
header ("Location: หน้าที่ต้องการ");
break;
}
ลองดูคำสั่ง Switch แบบนี้ดูก็ได้ค่ Debug ง่ายดี
Date :
2012-07-20 06:50:14
By :
Th@iDev
ขอบคุนน๊ะค๊ะ คุนกะล่อน ขอโทดด้วยจิง ๆๆ ค่ะ หนูแค่เครียดและอยากได้ไปหน่อย แล้วจะลองใหม่ค่ะTwT"
ขอบคุนน๊ะค๊ะ คุนTh@iDev แล้วจะลองดูค่ะ ^^
Date :
2012-07-20 12:13:27
By :
เด็กน้อย
ทำได้แล้วค่ะ ! !! เย้ เย้ ขอบคุนอีกครั้งน่ะค่ะ คุนกะล่อน ^^
Date :
2012-07-20 12:16:52
By :
เด็กน้อย
$_SESSION["Username"] = $objResult["Username"];
$_SESSION["Status"] = $objResult["Status"];
session_write_close();
$Username = substr($Username, 0, 7);
if ($Username==5210210)
{
header ("Location: index3.php");
exit(0);
}
elseif ($Username==5310210)
{
header ("Location: index2.php");
exit(0);
}
elseif ($Username==5410210)
{
header ("Location: index3.php");
exit(0);
}
if($objResult["Status"] == "ADMIN")
{
header("location:admin.php");
}
else
{
header("location:index2.php");
}
พี่กะล่อนค่ะ พอคนที่เป็น ADMIN เข้ามา มันไม่ลิ้งไปหน้า admin.php ให้ค่ะ
Date :
2012-07-20 12:21:49
By :
เด็กน้อย
Load balance : Server 02