View Full Version : problems with header() function

04-17-2012, 01:47 AM
I'm building a simple login form that references itself. I'm trying to redirect after a successful login, but my header function isn't working.

The username password query is working (verified by the echo statements) but I can't get the header function to work. Any ideas as to what is wrong?

require_once 'scripts/app_config.php';
require_once 'scripts/database_connection.php';


<!doctype html>


<!-- CSS -->
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/animate.css">
<link rel="stylesheet" href="css/login.css">



$username = $_POST['username'];
$password = $_POST['password'];

//Check if the username or password boxes were not filled in
if(!$username || !$password){
//if not display an error message
echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>";
// find user by username and password - working
$userQuery = 'SELECT * FROM users
WHERE user_name ='.'"'. $username.'" AND password='.'"'. $password.'"' ;
$users = mysql_query($userQuery);

$user = mysql_fetch_array($users);
// echo for testing - working - all values are printing
echo 'username:'.$user['user_id'].'<br />';
echo 'password:'.$user['password'].'<br />';
echo "<center>You have successfully logged in!</center>";

//set the login session storing there id - we use this to see if they are logged in or not
$_SESSION['user_id'] = $user['user_id'];
echo "session user_id: ".$_SESSION['user_id'].'<br />';
header("Location: http://aaronhaas.com/pitchshark6/index.php");

<div id="container">
<form action="" method="POST">

<label for="username">Username:</label>
<input type="name" name="username" id="username">

<label for="password">Password:</label>
<p><a href="#">Forgot your password?</a>
<input type="password" name="password" id="password">

<div id="lower">
<input type="checkbox"><label class="check" for="checkbox">Keep me logged in</label>
<input type="submit" value="Login">
</div> <!-- close lower -->



<!-- End Page Content -->



04-17-2012, 02:42 AM
Unless you are using output buffering, headers must be called before any output. This includes breaks in PHP code with HTML or even just whitespace, and any print/echos.