ฐานข้อมูล
username trainee1,staff1
password tra1234,sa1234
status trainee,staff
--------------------------------------------------------
โค้ดหน้า login.php
<?php
session_start();
// ติดต่อฐานข้อมูล
include("connect.php");
// รับค่า username และ password จากฟอร์ม
$username = $_POST['username']; // รับ username เข้ามา
$password = $_POST['password']; // รับ password เข้ามา
// เช็คว่าผู้ใช้คลิ๊กปุ่ม submit หรือยัง
if($_POST['submit'] == "Login"){
// เช็คว่าป้อนค่าในฟอร์มครบหรือยัง (ฟอร์มทั้ง 2 ช่อง ต้องไม่เป็นค่าว่าง)
if($username != "" and $password != ""){
// เขียนคำสั่ง sql ดึงข้อมูล email กับ password จากตาราง registermember
$sql = "SELECT lname,sname FROM personnel WHERE username='$username' and password='$password'";
$sqlquery = mysql_query($sql);
// นับแถวเพื่อเช็คว่ามี username และ password นี้อยู่หรือไม่
$num = mysql_num_rows($sqlquery);
// ดึงข้อมูลตามฟิลด์
$data = mysql_fetch_array($sqlquery);
// เช็คว่า username และ password ที่ป้อนเข้ามาถูกต้องหรือไม่
if($num >=1){
// สร้างตัวแปร session
//$_SESSION['sessionlogin'] = $username;
//$_SESSION['sessionlname'] = $data[lname];
//$_SESSION['sessionsname'] = $data[sname];
// ส่งไปหน้า member.php
//header("location:home.php");
echo "<script>location.href='check_login.php';</script>";
}else{
echo "<center><font color=red>คุณป้อน username หรือ password ไม่ถูกต้อง ลองใหม่!!</font></center>";
}
}else{ //ถ้าฟอร์มมีค่าว่าง
echo "<center><font color=red>กรุณาป้อนข้อมูลให้ครบก่อน</font></center>";
}
}
?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
Login to SystemWork<br>
<table border="1" style="width: 300px">
<tbody>
<tr>
<td> Username</td>
<td>
<input name="username" type="text" id="username">
</td>
</tr>
<tr>
<td> Password</td>
<td><input name="password" type="password" id="password">
</td>
</tr>
</tbody>
</table>
<br>
<input type="submit" name="submit" value="Login" id="submit">
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------------------
โค้ดหน้า check_login.php
<?
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
include("connect.php");
$sql = "select * from personnel where username = '$username' and password = '$password' ";
$result = mysql_db_query($dbname,$sql);
$row = mysql_num_rows($result);
$record = mysql_fetch_array($result);
$strUser = $username;
if($record[STATUS]=='staff'){
session_register("strUser");
echo "<script>location.href='staff.php';</script>";
}else if ($record[STATUS]=='trainee') {
session_register("strUser");
echo "<script>location.href='trainee.php';</script>";
}
?>
ขอสอบถามหน่อยครับคือหน้า login มัน login เข้าได้ตรวจสอบได้หมดแล้ว แต่เมื่อ เด็งเข้าหน้า check_login มันไม่ยอมเช็คสถานะผ็ใช้งานอะครับ ขอสอบถามหน่อยว่าผิดตรงไหนอย่างไรครับ
ช่วยดู โค้ด php หน่อยครับ
username trainee1,staff1
password tra1234,sa1234
status trainee,staff
--------------------------------------------------------
โค้ดหน้า login.php
<?php
session_start();
// ติดต่อฐานข้อมูล
include("connect.php");
// รับค่า username และ password จากฟอร์ม
$username = $_POST['username']; // รับ username เข้ามา
$password = $_POST['password']; // รับ password เข้ามา
// เช็คว่าผู้ใช้คลิ๊กปุ่ม submit หรือยัง
if($_POST['submit'] == "Login"){
// เช็คว่าป้อนค่าในฟอร์มครบหรือยัง (ฟอร์มทั้ง 2 ช่อง ต้องไม่เป็นค่าว่าง)
if($username != "" and $password != ""){
// เขียนคำสั่ง sql ดึงข้อมูล email กับ password จากตาราง registermember
$sql = "SELECT lname,sname FROM personnel WHERE username='$username' and password='$password'";
$sqlquery = mysql_query($sql);
// นับแถวเพื่อเช็คว่ามี username และ password นี้อยู่หรือไม่
$num = mysql_num_rows($sqlquery);
// ดึงข้อมูลตามฟิลด์
$data = mysql_fetch_array($sqlquery);
// เช็คว่า username และ password ที่ป้อนเข้ามาถูกต้องหรือไม่
if($num >=1){
// สร้างตัวแปร session
//$_SESSION['sessionlogin'] = $username;
//$_SESSION['sessionlname'] = $data[lname];
//$_SESSION['sessionsname'] = $data[sname];
// ส่งไปหน้า member.php
//header("location:home.php");
echo "<script>location.href='check_login.php';</script>";
}else{
echo "<center><font color=red>คุณป้อน username หรือ password ไม่ถูกต้อง ลองใหม่!!</font></center>";
}
}else{ //ถ้าฟอร์มมีค่าว่าง
echo "<center><font color=red>กรุณาป้อนข้อมูลให้ครบก่อน</font></center>";
}
}
?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
Login to SystemWork<br>
<table border="1" style="width: 300px">
<tbody>
<tr>
<td> Username</td>
<td>
<input name="username" type="text" id="username">
</td>
</tr>
<tr>
<td> Password</td>
<td><input name="password" type="password" id="password">
</td>
</tr>
</tbody>
</table>
<br>
<input type="submit" name="submit" value="Login" id="submit">
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------------------
โค้ดหน้า check_login.php
<?
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
include("connect.php");
$sql = "select * from personnel where username = '$username' and password = '$password' ";
$result = mysql_db_query($dbname,$sql);
$row = mysql_num_rows($result);
$record = mysql_fetch_array($result);
$strUser = $username;
if($record[STATUS]=='staff'){
session_register("strUser");
echo "<script>location.href='staff.php';</script>";
}else if ($record[STATUS]=='trainee') {
session_register("strUser");
echo "<script>location.href='trainee.php';</script>";
}
?>
ขอสอบถามหน่อยครับคือหน้า login มัน login เข้าได้ตรวจสอบได้หมดแล้ว แต่เมื่อ เด็งเข้าหน้า check_login มันไม่ยอมเช็คสถานะผ็ใช้งานอะครับ ขอสอบถามหน่อยว่าผิดตรงไหนอย่างไรครับ