Page 1 of 1

Unable to Redirect to Page

Posted: Fri Jan 28, 2011 1:48 pm
by jamgood96
I'm creating a basic login page for a database. Everything seems to be working fine with the exception that I cannot get it to redirect to a different page if a certain condition is met (found username & password). I have it setup to check the database for the username and password, and if the count is equal to one, to redirect to the main.php page. Unfortunately, it's not doing this.

Code: Select all

<style type="text/css">
body,td,th {
	font-family: Verdana, Geneva, sans-serif;
}
</style>

<?php // login.php

include 'functions.php';

// check if both username and password contain values
if(isset($_POST['username']) && isset($_POST['password']))
{

	// username and password sent from form
	$username = $_POST['username']; 
	$password = $_POST['password'];
	
	// protect MySQL injection and encrypt password
	$username = sanitizeString($username);
	$password = sanitizeString($password);
	$password = "1x3w5v7r" . $password . "c2e4b6t8";
	$password = md5('password');
	
	$query = "SELECT username,password FROM user_accounts
			WHERE username='$username' AND password='$password'";
			
	$count = mysql_num_rows(queryMysql($query));
	if($count==1)
	{
		// Register $username, $password and redirect to file "main.php"
		session_register("username");
		session_register("password"); 
		header("Location: /main.php");
		exit;
	}
	else
	{
		$error = $username = $password = "";
		$error = "<tr><td colspan='2' align='center' bgcolor='#FF6666'><font size='2' color='#FFFFFF'><b>invalid username/password</b></font></td></tr>";
	}
}

echo <<<_END
<center>
<br />
<form method='post' action='login.php'>
<table border="0" cellspacing="0" cellpadding="5">
	<tr>
		<td align="center" colspan="2" bgcolor="#FFCC66"><font size="6"><b>Database<b /></font></td>
	</tr>
	$error
  <tr>
    <td bgcolor="#E6E6E6" align="right">username</td>
    <td bgcolor="#E6E6E6"><input type='text' maxlength='16' name='username'
value='$username' /></td>
  </tr>
  <tr>
    <td bgcolor="#E6E6E6" align="right">password</td>
    <td bgcolor="#E6E6E6"><input type='password' maxlength='16' name='password' value='$password' /></td>
  </tr>
  <tr>
  	<td colspan="2" align="center"><input type='submit' value='       > > >     l  o  g  i  n     < < <       ' /></td>
  </tr>
</table>
</form>
</center>
_END;
?>

Re: Unable to Redirect to Page

Posted: Fri Jan 28, 2011 1:54 pm
by John Cartwright
Let me guess, your getting the "Headers already sent" error message. If you aren't, it's because you have error reporting disabled.

Regardless, this has been explained millions of times.

Re: Unable to Redirect to Page

Posted: Fri Jan 28, 2011 7:57 pm
by Pazuzu156
A non professional way to do this is in the code where you have them type in the username and password, it it's correct, just ad a meta tag.

Code: Select all

<?php if(isset($_POST['submit'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    //check if username and password have both been filled out, if not then run the else
	if($username && $password) {
		echo "Login in, redirecting you to main page.<meta http-equiv='refresh' content='3;url=index.php'>";
	} else {
		echo "Please fill out the entire form, thanks.";
	}
}
?>
<!--Login Form-->
<form method='POST' action='login.php'>
<table>
	<tr>
		<td>Username:</td>
		<td><input type='text' size='24' name='username' placeholder='Username' /></td>
	</tr>
	<tr>
		<td>Password:</td>
		<td><input type='password' size='24' name='password' placeholder='Password' /></td>
	</tr>
	<tr>
		<td><input type='submit' name='submit' value='Login' /></td>
		<td><input type='reset' value='Reset' /></td>
	</tr>
</table>
</form>
Of course this syntax does not call a certain username or password from a database, BUT the thing here is the <meta> tag.

<meta http-equiv="refresh" content="2;url=index.php">

http-equiv="refresh" this refreshes the current page

content="2;url=index.php" this refreshes after 2 seconds and loads to index.php

Hope this NON-PROFESSIONAL WAY helps you.

Re: Unable to Redirect to Page

Posted: Sat Jan 29, 2011 12:06 pm
by social_experiment
session_register() has been deprecated, you shouldn't rely on those. On successfull login regenerate the session and after that try your redirect.