Jump to content

[SOLVED] Header Problem "cannot modify header information"


frijole

Recommended Posts

I am working on a log in script and I am trying to redirect the user back to the front page once they have logged in. This is my log in Script, followed by the attached functions: I would really appreciate some guidance if anyone has seen this before.

 

<?php

require_once 'dbConnect.php';
require_once 'functions.php';

$error = '';

if ($_POST['logIn'] == 1) {	//if the form has been submitted

$userName = $_POST['user'];
$pass = $_POST['pass'];

$query = "SELECT * FROM users WHERE user_name='$userName' AND user_pass='$pass'";
$result = mysql_query($query) or die("connection error.");

if (empty($userName) || empty($pass)) {	//are any of the fields empty

	$error = "All fields must be filled out.";}

elseif (mysql_num_rows($result) != 1) { //if the username and password combo are not valid

	$error = "Invalid username, password combo.";}

else {	//log in was successful

	if (keepLogged ==1) {
		$year = 525600 + time();
		setcookie(thinksnack, 1, $year);	}

	$_SESSION['userName'] = $userName;
	$_SESSION['loggedIn'] = 1;
	header('Location: http://www.thinksnack.com/');

	}
}

showLogInForm ($error);

?>

 

 

<?PHP

function showRegisterForm ($error) {

echo $error;

?>

<fieldset>
    <legend>Register</legend>
    <table>
    <form name="input" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<tr>
<td>Username: </td>
<td><input type="text" name="user"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="rePass"></td>
</tr>
<tr>
    	<input type="hidden" name="register" value="1">
<td><input type="submit" value="Submit"></td>
</tr>
</form>
    </table>
    </fieldset>
<?PHP

}?>

<?PHP

function showLogInForm ($error) {

echo $error;

?>
<table>
    <form name="input" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<tr>
<td>username </td><td>password</td><td>stay logged in<input type="checkbox" name="keepLogged" value="1"></td>
</tr><tr>
    <td><input type="text" name="user"></td>
<td><input type="password" name="pass"></td>
   	<input type="hidden" name="logIn" value="1">
    <td><input type="submit" value="Submit"></td>
</tr>
</form>
    </table>
  		
<?php } ?>

it's because you're setting a cookie, which sends header information out to the browser.

 

You can get around this by buffering the output,

 

<?php
# Buffer the output
ob_start();

require_once 'dbConnect.php';
require_once 'functions.php';
    
$error = '';
    
if ($_POST['logIn'] == 1)
{   
    $userName = $_POST['user'];
    $pass = $_POST['pass'];
        
    $query = "SELECT * FROM users WHERE user_name='$userName' AND user_pass='$pass'";
    $result = mysql_query($query) or die("connection error.");
        
    if (empty($userName) || empty($pass))
    {   
        $error = "All fields must be filled out.";
    }
        
    elseif (mysql_num_rows($result) != 1)
    { 
        $error = "Invalid username, password combo.";
    }
    else
    {
        if (keepLogged ==1)
        {
            $year = 525600 + time();
            setcookie(thinksnack, 1, $year);
        }
    
        $_SESSION['userName'] = $userName;
        $_SESSION['loggedIn'] = 1;
        
        header('Location: http://www.thinksnack.com/');                    
    }
}
    
showLogInForm ($error);

# Dump the buffer
ob_end_flush(); 
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.