Page 1 of 1

header error

Posted: Tue Jan 06, 2004 7:43 pm
by CraniumDesigns
i'm getting this error...

Warning: Cannot modify header information - headers already sent by (output started at /home/steve/public_html/christian/header.inc:7) in /home/steve/public_html/christian/checkuser.php on line 39

here is the code:

Code: Select all

<?

session_start();
header("Cache-control: private"); // IE 6 Fix

include 'header.inc';

/* Check User Script */

// Convert to simple variables
$username = $_POST['username'];
$password = $_POST['password'];

if((!$username) || (!$password)){
	echo "Please enter ALL of the information! <br />";
	include 'login_form.html';
	exit();
}

// Convert password to md5 hash
$password = md5($password);

// check if the user info validates the db
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'");
$login_check = mysql_num_rows($sql);

if($login_check > 0){
	while($row = mysql_fetch_array($sql)){
	foreach( $row AS $key => $val ){
		$$key = stripslashes( $val );
	}
		// Register some session variables!
		session_register('first_name');
		$_SESSION['first_name'] = $first_name;
		session_register('email_address');
		$_SESSION['email_address'] = $email_address;
		
		mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'");
		
		header("Location: login_success.php");
	}
} else {
	echo "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br />
	Please try again!<br />";
	include 'login_form.html';
}

include 'footer.inc';

?>
?>

Posted: Tue Jan 06, 2004 8:05 pm
by Gen-ik
It looks like it's because you are using header("Location: login_success.php"); after something has been echoed() or printed() to the browser.

Unless you are buffering the page then all headers must be sent before anything is dumped to the browser.

Check out [php_man]ob_start[/php_man] (and related functions) if you want to find out more about page buffering.

Posted: Tue Jan 06, 2004 8:18 pm
by scorphus
Cranium, our mate Gen-ik resumed what you can read on this tutorial: Warning: Cannot add header information.

Regards,
Scorphus.

Posted: Tue Jan 06, 2004 8:18 pm
by CraniumDesigns
this authenticates the login form info and then goes to the login_success.php page, how do i move the header command so it still works?

Posted: Tue Jan 06, 2004 8:27 pm
by scorphus
Maybe try to move

Code: Select all

header("Cache-control: private"); // IE 6 Fix

and

Code: Select all

include 'header.inc';
(if it contains some echo or if it is pure HTML) after

Code: Select all

header("Location: login_success.php");
Tell us what you get.

Cheers,
Scorphus.

Posted: Tue Jan 06, 2004 8:28 pm
by CraniumDesigns
thanks scorphus. that worked perfectly.

Posted: Tue Jan 06, 2004 8:29 pm
by CraniumDesigns
well, not that last suggestion, but that link did. thanks.

Posted: Tue Jan 06, 2004 8:33 pm
by scorphus
Welcome, I'm glad it helps!