header error

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
CraniumDesigns
Forum Newbie
Posts: 18
Joined: Fri Nov 07, 2003 1:35 am

header error

Post 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';

?>
?>
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Cranium, our mate Gen-ik resumed what you can read on this tutorial: Warning: Cannot add header information.

Regards,
Scorphus.
CraniumDesigns
Forum Newbie
Posts: 18
Joined: Fri Nov 07, 2003 1:35 am

Post 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?
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
CraniumDesigns
Forum Newbie
Posts: 18
Joined: Fri Nov 07, 2003 1:35 am

Post by CraniumDesigns »

thanks scorphus. that worked perfectly.
CraniumDesigns
Forum Newbie
Posts: 18
Joined: Fri Nov 07, 2003 1:35 am

Post by CraniumDesigns »

well, not that last suggestion, but that link did. thanks.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Welcome, I'm glad it helps!
Post Reply