Login page problem

arang67

Registered Member
Joined
Feb 14, 2012
Messages
90
Reaction score
28
hii all i want to login
but the problem is i enter the username and password it says failed.
im very sure the username and password is correct.anyone can explain why this happen?

<?phprequire_once 'database.php';


session_start();


function signIn()
{
$sql = 'SELECT username, user_type FROM users WHERE username = "'.addslashes($_POST['username']).'" ';
$sql .= 'AND password = "'.addslashes(md5($_POST['password'])).'";';
dbConnect();
$result = runQuery($sql);


if (mysql_num_rows($result) != 0)
{
$data = mysql_fetch_assoc($result);
$_SESSION['username'] = stripslashes($data['username']);
$_SESSION['user_type'] = $data['user_type'];


if ($_SESSION['user_type'] == 'student')
{
header('Location: studentpage.php');
}
else if ($_SESSION['user_type'] == 'staff')
{
header('Location: adminpage.php');
}
}
else {
header('Location: index2.php?status=SIGN-IN-FAILED');


}
}


function signOut()
{
session_destroy();
header('Location: index2.php');
}


function isSessionValid()
{
if (!isset($_SESSION['username']) || empty($_SESSION['username'])) {
signOut();
}


return true;
}


if ($_POST['action'] == 'login')
{
signIn();
}


if ($_GET['logout'] == 'true')
{
signOut();
}
?>
 
According to your code, this script is checking the database for the username, user type and password and matching it to what was typed into the form. The script is returning as failed since it hasn't found any users that exactly match the user information you typed into the form. The script could be failing for any number of reasons. Is there any chance you'd be able to post your database structure and the HTML of the form so we can help you more with this?
 
This isn't really the best way to do this. There are a few problems that I see here.


1. you should be registering your variables to the session
2. you should be using mysql_real_escape_string() instead of addslashes()
3. i can't see your mysql connection functions, but you should have mysql_error() in there to see what mysql is telling you cuz it's clearly a problem with your MySQL statement
4. your MySQL statement should be putting the table in `` quotes, so try it like `users`
 
Back
Top