phpfan28 Posted April 13, 2012 Share Posted April 13, 2012 I having trouble with my re-direct code on my php registration form. The form does submit data to the database but I'm getting the following error. "Warning: Cannot modify header information - headers already sent by (output started at /homepages/25/d232402382/htdocs/testencourage/registration.php:15) in /homepages/25/d232402382/htdocs/testencourage/registration.php on line 87" I would like the form goes to the thanks.php after submitting the data but its not doing its job. Here is the code. <?php if ($_SERVER['HTTPS']) { header('location: https://www.fakewebsite.com'); } the regular html syntax <title>, site navigation, etc... ?> <?php if (isset($_POST['submitted'])){ $fields = array( 'email', 'state', 'district', 'gender', 'age', 'profession', ); if (safe($_POST['survey']=="Yes")){ $survey = "Yes"; } else{ $survey = "No"; } foreach($fields as $fieldName) { if(isset($_POST[$fieldName]) and safe(trim(stripslashes($_POST[$fieldName]))) !==''){ $$fieldName = safe(trim(stripslashes($_POST[$fieldName]))); }else { $errors[] = "Please enter your". $fieldName .""; //code to validate fields } } if(!isset($errors)){ require_once('Connections/encourage.php'); $query = "INSERT INTO participants (email, state, district, gender, age, profession, survey, registration_date) VALUES ('$email', '$state', '$district', '$gender', '$age', '$profession','$survey', NOW())"; //databasse connection $result = mysql_query ($query); if ($result){ $url = 'http://'. $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); if ((substr($url, -1) == '/') || (substr($url, -1) == '\\')) { $url = substr ($url, 0 -1); } $url .= '/thanks.php'; header("Location: $url");// this is line 87 exit(); }else{ echo '<h1 id="mainhead">System Error</hl> <p>Your registration could not be completed due to a system error We apologize for any incovience</p>';//gives system error echo 'p' . mysql_error(). '<br /><br />Query: ' . $query . '</p>'; exit(); } mysql_close(); } else { echo '<h1 id="mainhead">Error!</h1> <p class="error">The following error(s) occurred:<br />'; foreach($errors as $msg) { echo " - $msg<br/>\n"; } echo '</p><p>Please try again.</p><p><br/></p>'; } } function safe($string) { $pattern = "/\r|\n|\%0a|\%0d|Content\-Type:|bcc:|to:|cc:/i"; return preg_replace($pattern, '', $string); } ?> Also I'm going to set up my ssl on the server so I'm planning to use that https redirect syntax but since I'm very new at this, am I missing something. Your help will be greatly appreciated, thanks! Quote Link to comment Share on other sites More sharing options...
freelance84 Posted April 14, 2012 Share Posted April 14, 2012 Basically you cannot echo anything before using the header function. You cannot send one header out to the client in the form of echo, then sent out a header to relocate in between. You can relocate, or, echo... that is what the error is telling you. Also, after header function it is always best to exit(); your php scripts... prevents anything from running any further causing unforeseen bugs. Quote Link to comment Share on other sites More sharing options...
freelance84 Posted April 14, 2012 Share Posted April 14, 2012 http://symfony.com/doc/current/book/http_fundamentals.html This starter on the symfony framework has a pretty decent illustration on the concept of headers Quote Link to comment Share on other sites More sharing options...
phpfan28 Posted April 15, 2012 Author Share Posted April 15, 2012 Okay, since I'm still new to php, could you provide me where the exact spot I should put the header() function, that will help me a lot since I'm a little lost. Thanks! Quote Link to comment 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.