I have a fairly basic login.php page which checks the username and password against a simple database. When successful, it should redirect the user to a members.php page. When I uploaded it to a live server, the code will not redirect. It stays on the login.php page which then just becomes a blank page. This surprised me because it had worked perfectly when I tried with my local apache server at home.
In the end I emailed the hosting company to find out if there was anything I needed to do with the code to make it work on their server. In their reply they said I needed to remove any echo/print statements in the script and to include an exit; statement after the header redirection.
As you can see in the code below, there are no echo/print statements in the script, and I have included an exit statement as they recommended. But it still does not work on their server.
So I have two questions I am hoping to have answered.
1. How to get this page working on their server - any clues at all will be gratefully recieved.
2. Why does it work on the local server, but not on the live server - worth knowing for future reference.
CODE
<?php
require("config.php");
require("dbconnect.php");
// check if there is a login cookie
if(isset($_COOKIE['my_ID'])) {
// if a valid one exists, it logs you in and directs you to the members page
$username = $_COOKIE['my_ID'];
$pass = $_COOKIE['my_PASS'];
$check = mysql_query("SELECT * FROM tbl_users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check )) {
if ($pass != $info['password']) {
} // end if
else {
header("Location: members.php");
} // end else
} // end while
} // end if login cookie
//if the login form is submitted but there is no login cookie yet
if (isset($_POST['submit'])) { // if form has been submitted
// make sure the fields are both filled in
if(!$_POST['username'] | !$_POST['pass']) {
die('You did not fill in a required field.');
} // end if
// check it against the database
$check = mysql_query("SELECT * FROM tbl_users WHERE username = '" . $_POST['username'] . "'") or die(mysql_error());
//Give error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database.'); // <a href=add.php>Click Here to Register</a>');
} // end if
while($info = mysql_fetch_array( $check )) {
//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die('Incorrect password, please try again.');
} // end if password wrong
else { // if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(my_ID, $_POST['username'], $hour);
setcookie(my_PASS, $_POST['pass'], $hour);
//then redirect them to the members area
header("location: members.php");
exit;
} // end else
} // end while
} // end if form submitted
?>
<html>
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Login Page</h1></td></tr>
<tr><td>Please type your username and password in the boxes below and click the submit button</td></tr>
</table><br /><br />
<table border="0">
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
</body>
</html>
If you feel like commenting on how bad some of the code is, I will not take offence as I am hoping to learn as much as possible here
many thanks in advance