knobby2k Posted August 22, 2011 Share Posted August 22, 2011 Hi guys, I've got a header problem. I've read the header sticky post but i'm still not getting anywhere. I am currently working on a registration form which loops though checking the data entered meets certain criteria by calling various functions (min length, max length, preg match, etc...), if anything returns 'false' then the form is reproduced with the error message of why it failed and what needs to be changed. If everything returned is 'true' then all day matches what is expected and the code then runs an 'insert' command. If the insert is successful then the code should forward the user to a new page displaying 'registration complete'. However, the way I have written my code I am finding it near impossible to get the forward to be before anything is output to the browser therefore causing the header error. I have tried moving things around but i'm having no luck. The error I am getting is: Warning: Cannot modify header information - headers already sent by (output started at /websites/blahblahblah/register.php:6) in /websites/blahblahblah/register.php on line 77 The code there is relevant though as it is the code that make the connection to my DB. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php // *** Connection - START *** // connect to database $connect = @mysql_connect("test", "test", "test") or die(mysql_error()); if (!$connect) { do_error("Could not connect to the server"); } @mysql_select_db("test_db",$connect)or do_error("Could not connect to the database"); // *** Connection - END *** // This file includes all of the check functions for the code to run through include("check.php"); if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) { $data= new check($_POST); $data->name('forename'); $data->name('surname'); if ( $data->pass == true ) { // Now we have the required data in the correct format do the code required to complete registration $forename=$data->input['forename']; $surname=$data->input['surname']; $query="INSERT INTO users ( forename, surname ) VALUES ('$forename' , '$surname' )"; if (mysql_query($query,$connect)) { //echo "success"; // once data is inserted, go to complete page header ("location: complete.php"); exit; } else { echo "Error: Please try again."; } } } else { $data= new check(); } ?> What am I actually doing wrong and how can i possibly get the insert script higher up the code?? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/245426-header-problem/ Share on other sites More sharing options...
titan21 Posted August 22, 2011 Share Posted August 22, 2011 Try ob_start() and ob_flush() - this will stop anything being sent to the browser before the request completes http://uk3.php.net/manual/en/function.ob-start.php Quote Link to comment https://forums.phpfreaks.com/topic/245426-header-problem/#findComment-1260520 Share on other sites More sharing options...
AbraCadaver Posted August 22, 2011 Share Posted August 22, 2011 Get rid of all that HTML at the beginning of the page. Quote Link to comment https://forums.phpfreaks.com/topic/245426-header-problem/#findComment-1260532 Share on other sites More sharing options...
titan21 Posted August 22, 2011 Share Posted August 22, 2011 Ha ha! I obviously wasn't paying attention - yeh AbraCadaver is right - u don't need that HTML! Quote Link to comment https://forums.phpfreaks.com/topic/245426-header-problem/#findComment-1260534 Share on other sites More sharing options...
knobby2k Posted August 22, 2011 Author Share Posted August 22, 2011 Hi guys, Thanks for your replies. The code i've posted is only a snippet, the full file is about 300 lines of code and has a lot of other html so won't i need those first 4-5 lines?? Also the link that you sent me didn't work titan. Where would i start and end the ob_start/ob_flush? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/245426-header-problem/#findComment-1260536 Share on other sites More sharing options...
Muddy_Funster Posted August 22, 2011 Share Posted August 22, 2011 Cut and paste your HTML so that is is AFTER your header scripts. You can NOT run a header once output has been sent to a page (regardless of if you can see it or not). Quote Link to comment https://forums.phpfreaks.com/topic/245426-header-problem/#findComment-1260562 Share on other sites More sharing options...
titan21 Posted August 22, 2011 Share Posted August 22, 2011 If you absolutely need to splice your pHP with your HTML, then place ob_start() as the first line of PHP code, above your HTML DOCTYPEs etc and then the last statement, after everything else, should be ob_flush(). Quote Link to comment https://forums.phpfreaks.com/topic/245426-header-problem/#findComment-1260571 Share on other sites More sharing options...
knobby2k Posted August 22, 2011 Author Share Posted August 22, 2011 You lot are amazing! Cheers for all your help it's now sorted!! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/245426-header-problem/#findComment-1260574 Share on other sites More sharing options...
JasonLewis Posted August 22, 2011 Share Posted August 22, 2011 If you're using output buffering to fix this you shouldn't be. The code you're running should be placed before any output anyway, as it is doing nothing with/to output. Output buffering isn't a solution to this problem, it's a band aid. Quote Link to comment https://forums.phpfreaks.com/topic/245426-header-problem/#findComment-1260584 Share on other sites More sharing options...
AbraCadaver Posted August 22, 2011 Share Posted August 22, 2011 I agree. You can mask the issue with output buffering, but it's a better practice to just code it correctly. Why output and buffer it if you're never going to use it? Quote Link to comment https://forums.phpfreaks.com/topic/245426-header-problem/#findComment-1260623 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.