Hi All
I'm trying to get my login script to work with IE (I used firefox when developing it and everything works fine in this).
The problem is that when I try and login with IE, rather than redirecting me to the user area, IE loops back and redisplays the login script. I've so far worked out that IE isn't picking up the action == login bit that I try to post back to the same page to perform the login.
Does anyone have any pointers as to why this causes IE to loop, rather than login? Interestingly, it seems to pick up the user id when it loops back to itself (i.e. displays the user post value back in the username login box).
Here is the code of the page (i've intentionally blanked out the d/b host information)
<?php
// these are the database settings
$cfg['host'] = "xxx";
$cfg['user'] = "xxx";
$cfg['pass'] = "xxx";
$cfg['database'] = "xxx";
// This is the location to send the user after successfully login
$cfg['onSuccess'] = "./secure.php";
// This is the location to send the user after clicking cancel
$cfg['onCancel'] = "./index.php";
// starts the session to track the user variables.
session_start();
// the name of the users table
$cfg['usersTable'] = "users";
// Connect and select the database
mysql_connect($cfg['host'],$cfg['user'],$cfg['pass']) or die ('Could not connect to localhost');
mysql_select_db($cfg['database']);
// process the login request
if($_POST['action'] == "login")
{
// check to see if the user field or pass field is empty. if so set message.
if(empty($_POST['user']) || empty($_POST['pass']))
{
// user or pass was empty. set the message text
$message = "You must enter a valid username and password!";
}
else
{
$_POST['pass'] = md5($_POST['pass']);
// query the users table
$query = mysql_query("SELECT * FROM ".$cfg['usersTable']." WHERE u_mobile='".$_POST['user']."' AND u_pass='".$_POST['pass']."' LIMIT 1") or die('query failed!');
// did the query return a user
if(mysql_num_rows($query) == 1)
{
// set the session variables with the user data
while($row = mysql_fetch_assoc($query))
{
$_SESSION['auth']['ID'] = $row['u_id'];
$_SESSION['auth']['mobile'] = $row['u_mobile'];
$_SESSION['auth']['forename'] = $row['u_forename'];
$_SESSION['auth']['surname'] = $row['u_surname'];
$_SESSION['auth']['title'] = $row['u_title'];
$_SESSION['auth']['password'] = $row['u_password'];
$_SESSION['auth']['email'] = $row['u_email'];
$_SESSION['auth']['class'] = $row['u_class'];
$_SESSION['auth']['status'] = 1;
}
// login was successfull. redirect to the onSuccess location
header("Location: ".$cfg['onSuccess']);
exit();
}
else
{
// user did not exist. set the message text
$message = "<B>User does not exist.</B><br>Check your username and password.".$query."";
}
}
// do this if the logout command is set (action=logout)
}
elseif($_GET['action'] == "logout")
{
// unset the authentication session variable
unset($_SESSION['auth']);
// redirect to the onCancel location
header("Location: ".$cfg['onCancel']);
exit();
}
if($_SESSION['auth']['status'] != 1)
{
//display login form if the user isn't logged in
//was $_SERVER['PHP_SELF']
Echo"<form method='post' action='login.inc.php'";
Echo"<input type='hidden' name='action' value='login'>";
Echo"<table align='center'>";
Echo"<tr>";
Echo"<td style='text-align:center;'>";
Echo"<table width='250'>";
if(isset($message))
{
echo "<tr><td colspan='2'>" . $message . "</td></tr>";
}
Echo"<tr>";
Echo"<td colspan='2' align='center'>Login Here</td>";
Echo"</tr>";
Echo"<tr>";
Echo"<td width='50%'>Mobile:</td>";
Echo"<td width='50%'><input type='text' name='user' value= " . $_POST['user'] . "></td>";
Echo"</tr>";
Echo"<tr>";
Echo"<td width='50%'>Password:</td>";
Echo"<td width='50%'><input type='password' name='pass'><td>";
Echo"</tr>";
Echo"<tr>";
Echo"<td colspan=2 align=center>";
Echo"<input type='submit' value='login'>";
//Echo"<input type='button' value='Cancel' onClick='window.location=" . $cfg['onCancel'] . ">";
Echo"</td>";
Echo"</tr>";
Echo"</table>";
Echo"</form>";
exit;
}
?>
Any help getting this to work would be greatly appreciated - i've spent hours reading up (on forms, post, headers, actions etc..) and haven't found a good reason why firefox is cool with this code and IE isn't.