Cannot modify header information

Hiho, need some help :wink:
I just got this error, don’t know how to fix it… any help is appreciated.

Warning: Cannot modify header information - headers already sent by (output started at /home/virts/public_html/includes/overall/header.php:5) in /home/virtous/public_html/register.php on line 51

My code:
[php]<?php
include ‘core/init.php’;
include ‘includes/overall/header.php’;

if(empty($_POST) === false) {
$required_fields = array(‘username’, ‘password’, ‘password_again’, ‘email’, ‘email_again’);
foreach($_POST as $key=>$value) {
if(empty($value) && in_array($key, $required_fields) === true ) {
$errors[] = ‘Fields marked * are required.’;
break 1;
}
}

if(empty($errors) === true) {
	
	if(user_exists($_POST['username']) === true) {
		$errors[] = 'Sorry, the username \'' . $_POST['username'] . '\' is already in use.';
	}
	if(preg_match("/\\s/", $_POST['username']) == true) {
		$errors[] = 'Username may not contain any spaces.';
	}
	if(strlen($_POST['password']) < 6) {
		$errors[] = 'Password must be at least 6 characters.';
	}
	if($_POST['password'] !== $_POST['password_again']) {
		$errors[] = 'Passwords don\'t match.';
	}
	if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
		$errors[] = 'A valid email address is required.';
	}
	if(email_exists($_POST['email']) === true){
		$errors[] = 'Sorry, this email \'' . $_POST['email'] . '\' is already in use.';
	} 
}

}
print_r($errors);
?>

	<div id="Main_Wrapper">
		<?php 
if(empty($_POST) === false && empty($errors) === true) {
	$register_data = array(
		'username'		=> $_POST['username'],
		'password'		=> $_POST['password'],
		'first_name'	=> $_POST['first_name'],
		'last_name'		=> $_POST['last_name'],
		'email'			=> $_POST['email']
	);
	register_user($register_data);
	header('Location: register.php?success');     <----------------------------------------------Problem here
	exit();	
	

} else if (empty($errors) === false) {
	echo output_errors($errors);
	
}	 

?>


Register


* are required fields

				<br>
				<div id="Registration_form">
					<form action="" method="post">
							<h3 class="register">* Username:</h3> <input type="text" class="input2" name="username"> <br>
							<h3 class="register">* Password:</h3> <input type="password" class="input2" name="password"> <br>
							<h3 class="register">* Confirm password:</h3> <input type="password" class="input2" name="password_again"><br>
							<h3 class="register">* Email:</h3> <input type="text" class="input2" name="email"><br>
							<h3 class="register">* Confirm email:</h3> <input type="text" class="input2" name="email_again"><br>
							<h3 class="register">First name:</h3> <input type="text" class="input2" name="first_name"><br>
							<h3 class="register">Last name:</h3> <input type="text" class="input2" name="last_name"><br>
							<input type="submit" class="submit2" value="Register">
					</form>	
				</div>
			</div>
<?php if (logged_in() === true){ include 'includes/widgets/loggedin.php'; } else { include 'includes/widgets/login.php'; } include 'includes/panel_right.php'; ?>
	</div>			
<?php include 'includes/overall/footer.php'; ?>[/php]

Update:
I added ob_start(); at the top of the page and ob_end_flush(); at the end of the page, now I get redirected to the register.php?success page without getting the error message. All the content is displayed. However, for some reason it doesn’t add the user to the database…

the function I use to add users is in a separate file and looks like this:

[php]function register_user($register_data) {
array_walk($register_data, ‘array_sanitize’);
$register_data[‘password’] = md5($register_data[‘password’]);

	$fields = '`' . implode('`, `', array_keys($register_data)) . '`';
	$data = '\'' . implode('\', \'', $register_data) . '\'';
	
	mysql_query("INSERT INTO `users` WHERE ($fields) VALUES ($data)");
}[/php]

Here is the updated register.php page
[php]<?php
ob_start();
include ‘core/init.php’;
include ‘includes/overall/header.php’;

if(empty($_POST) === false) {
$required_fields = array(‘username’, ‘password’, ‘password_again’, ‘email’, ‘email_again’);
foreach($_POST as $key=>$value) {
if(empty($value) && in_array($key, $required_fields) === true ) {
$errors[] = ‘Fields marked * are required.’;
break 1;
}
}

if(empty($errors) === true) {
	
	if(user_exists($_POST['username']) === true) {
		$errors[] = 'Sorry, the username \'' . $_POST['username'] . '\' is already in use.';
	}
	if(preg_match("/\\s/", $_POST['username']) == true) {
		$errors[] = 'Username may not contain any spaces.';
	}
	if(strlen($_POST['password']) < 6) {
		$errors[] = 'Password must be at least 6 characters.';
	}
	if($_POST['password'] !== $_POST['password_again']) {
		$errors[] = 'Passwords don\'t match.';
	}
	if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
		$errors[] = 'A valid email address is required.';
	}
	if(email_exists($_POST['email']) === true){
		$errors[] = 'Sorry, this email \'' . $_POST['email'] . '\' is already in use.';
	} 
}

}
print_r($errors);
?>

<?php if(empty($_POST) === false && empty($errors) === true) { $register_data = array( 'username' => $_POST['username'], 'password' => $_POST['password'], 'first_name' => $_POST['first_name'], 'last_name' => $_POST['last_name'], 'email' => $_POST['email'] ); register_user($register_data); header('Location: register.php?success'); exit();
} else if (empty($errors) === false) {
	echo output_errors($errors);	
}	

?>

Register

* are required fields

<br>
<div id="Registration_form">
	<form action="" method="post">
			<h3 class="register">* Username:</h3> <input type="text" class="input2" name="username"> <br>
			<h3 class="register">* Password:</h3> <input type="password" class="input2" name="password"> <br>
			<h3 class="register">* Confirm password:</h3> <input type="password" class="input2" name="password_again"><br>
			<h3 class="register">* Email:</h3> <input type="text" class="input2" name="email"><br>
			<h3 class="register">* Confirm email:</h3> <input type="text" class="input2" name="email_again"><br>
			<h3 class="register">First name:</h3> <input type="text" class="input2" name="first_name"><br>
			<h3 class="register">Last name:</h3> <input type="text" class="input2" name="last_name"><br>
			<input type="submit" class="submit2" value="Register">
	</form>	
</div>
<?php if (logged_in() === true){ include 'includes/widgets/loggedin.php'; } else { include 'includes/widgets/login.php'; }
include 'includes/panel_right.php'; 

?>

<?php include 'includes/overall/footer.php'; ob_end_flush(); ?>[/php]

Usually it’s an inappropriate placement of ‘session_start();’ that causes that, but anything that causes headers to be sent twice can do this.

I don’t think output buffer is going to solve this. It is just a warning and is not fatal.

Your original error is because you are trying to send headers with the [php]header(‘Location: register.php?success’);[/php] AFTER already sending stuff to the users browser:

[php]
print_r($errors);
[/php]

Also if header.php has any echo’s or something that will send anything to the users browser, the same will happen.

Sponsor our Newsletter | Privacy Policy | Terms of Service