Unable to Redirect to Page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamgood96
Forum Newbie
Posts: 12
Joined: Fri Jan 21, 2011 12:19 am

Unable to Redirect to Page

Post 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;
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Unable to Redirect to Page

Post 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.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Unable to Redirect to Page

Post 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.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Unable to Redirect to Page

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply