chiba Posted August 25, 2012 Share Posted August 25, 2012 Why am I getting this error message after I fill out the form and press submit? The data is entered correctly into the database, but the header doesn't work. :'( Warning: Cannot modify header information - headers already sent by (output started at /home/content/27/9433527/html/site/includes/overall/header.php:3) in /home/content/27/9433527/html/site/register.php on line 69 <?php include 'core/init.php'; include 'includes/overall/header.php'; if(empty($_POST)===false) { $required_fields = array('username','password','password_again','first_name','last_name','email'); foreach ($_POST as $key=>$value) { if (empty($value) && in_array($key, $required_fields)===true) { //if one error is found then break out $errors[] = 'Fields marked with an asterisk are required'; break 1; } } if(empty($errors)===true) { // triple === type checks as well as the value if (user_exists($_POST['username']) ===true) { $errors[] = 'Sorry the username \''.htmlentities($_POST['username']) . '\' is already taken.'; } if (preg_match("/\\s/",$_POST['username']) == true) { $errors[] = 'Your username must not contain any spaces.'; } if (strlen($_POST['password']) < 6) { $errors[] = 'Your password must be at least 6 characters'; } if ($_POST['password'] !== $_POST['password_again']) { $errors[] = 'Your passwords do not 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 the email \'' .$_POST['email'] . '\' is already in use'; } } } ?> <h1>Register</h1> <?php if (isset($_GET['sucess']) && empty($_GET['sucess'])) { echo 'You\'ve been registered sucessfully.'; } else { 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?sucess'); exit(); } else if (empty($errors) === false) { echo output_errors($errors); } ?> <p>Please fill out the form below to register an account.</p> <form action="" method="post"> <ul> <li> Username*:<br /> <input type="text" name="username"> </li> <li> Password*<br /> <input type="password" name="password"> </li> <li> Password again*<br /> <input type="password" name="password_again"> </li> <li> First Name*:<br /> <input type="text" name="first_name"> </li> <li> Last Name*:<br /> <input type="text" name="last_name"> </li> <li> Email address*:<br /> <input type="text" name="email"> </li> <li> <input type="submit" value="Register"> </li> </ul> </form> <?php } include 'includes/overall/footer.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/267570-warning-cannot-modify-header-information/ Share on other sites More sharing options...
Christian F. Posted August 25, 2012 Share Posted August 25, 2012 Please read this thread: http://forums.phpfreaks.com/index.php?topic=37442.0 Quote Link to comment https://forums.phpfreaks.com/topic/267570-warning-cannot-modify-header-information/#findComment-1372395 Share on other sites More sharing options...
chiba Posted August 26, 2012 Author Share Posted August 26, 2012 I solved it by adding ob_start( ); to my init.php file. Quote Link to comment https://forums.phpfreaks.com/topic/267570-warning-cannot-modify-header-information/#findComment-1372531 Share on other sites More sharing options...
trq Posted August 26, 2012 Share Posted August 26, 2012 nice hack. Quote Link to comment https://forums.phpfreaks.com/topic/267570-warning-cannot-modify-header-information/#findComment-1372535 Share on other sites More sharing options...
darkfreaks Posted August 26, 2012 Share Posted August 26, 2012 ob_start is a band aid fix at best you really should output all your HTML/CSS before your code. Quote Link to comment https://forums.phpfreaks.com/topic/267570-warning-cannot-modify-header-information/#findComment-1372547 Share on other sites More sharing options...
MMDE Posted August 26, 2012 Share Posted August 26, 2012 ob_start is a band aid fix at best you really should output all your HTML/CSS before your code. You mean the other way around, right? Quote Link to comment https://forums.phpfreaks.com/topic/267570-warning-cannot-modify-header-information/#findComment-1372553 Share on other sites More sharing options...
darkfreaks Posted August 26, 2012 Share Posted August 26, 2012 oh yeah sorry the first way they showed was the bad example not the example where it correctly showed to run the code then display the html and form then echo result. Quote Link to comment https://forums.phpfreaks.com/topic/267570-warning-cannot-modify-header-information/#findComment-1372557 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.