jarvis Posted March 17, 2010 Share Posted March 17, 2010 Hi All, The following code is an include on my site. What I'm after is a redirect, using headers is the best & quickest method, however, whenever I try this, I get the damned headers sent message. Is this because it's an include file? here's my code: <?php require_once "/lib/viewer_functions.php"; // process form if (@$_REQUEST['submitForm']) { // error checking $errorsAndAlerts = ""; $check = $_REQUEST['check']; $randomNumber = $_REQUEST['randomNumber']; if (!@$_REQUEST['name']) { $errorsAndAlerts = "Please add your name\n"; } if (!@$_REQUEST['email']) { $errorsAndAlerts = "Please add your email\n"; } else if(!isValidEmail(@$_REQUEST['email'])) { $errorsAndAlerts = "That email address is not valid\n"; } if (!@$_REQUEST['enquiry']) { $errorsAndAlerts = "Please add your enquiry\n"; } if (!@$_REQUEST['randomNumber']) { $errorsAndAlerts = "Please verify the number\n"; } $textToConvert = $_REQUEST['enquiry']; $convertedText = iconv("UTF-8", "ISO-8859-1", $textToConvert); // send email user if ((!$errorsAndAlerts) && ($randomNumber == $check)) { $to = "info@domain.com"; $subject = "Quick Contact"; $email = "info@domain.com"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $email \r\n"; $body = "<strong>Full name</strong>: ". $_REQUEST['name'] ."<br />"; $body .= "<strong>Email</strong>: ".$_REQUEST['email'] ."<br />"; $body .= "<strong>Enquiry</strong>: $convertedText<br /><br />"; $body .= "The user who sent this message had the IP address <strong>".$_SERVER['REMOTE_ADDR']."</strong><br />"; $message = $body; // send message $mailResult = @mail($to, $subject, $message, $headers); if (!$mailResult) { die("Mail Error: $php_errormsg"); } // //$errorsAndAlerts = "Thanks! We've sent your email."; header("Location: /thanks.php"); exit(); $_REQUEST = array(); // clear form values } } ?> <?php $randomNumber = rand(1,10); ?> <!-- right quick_contact form --> <!-- quick_contact_title --> <div class="rh_quick_contact_title"> <img src="images/rh_quick_enquiry_title.jpg" width="232" height="31" /> </div> <!-- /quick_contact_title --> <!-- quick_contact_form --> <div class="rh_quick_contact_content_larger"> <form method="post"> <input type="hidden" name="submitForm" value="1" /> <?php if (@$errorsAndAlerts): ?> <div style="color: red; font-weight: bold; font-size: 12px; font-family: arial;"> <?php echo $errorsAndAlerts; ?> </div> <?php endif ?> <table width="213" border="0" cellpadding="2" cellspacing="2"> <tr> <td align="center"> <input name="name" type="text" id="name" style="width:180px;" value="Name..." onclick="clickclear(this, 'Name...')" onblur="clickrecall(this,'Name...')" /> </td> </tr> <tr> <td align="center"> <input name="email" type="text" id="email" style="width:180px;" value="Email Address..." onclick="clickclear(this, 'Email Address...')" onblur="clickrecall(this,'Email Address...')"/> </td> </tr> <tr> <td align="center"> <textarea name="enquiry" id="enquiry" value="Enquiry..." onclick="clickclear(this, 'Enquiry...')" onblur="clickrecall(this,'Enquiry...')" style="width:180px;" rows="3"></textarea> </td> </tr> <tr> <td align="center"><input type="text" id="randomNumber" name="randomNumber" style="width:20px;" /> Please enter the following number in the left hand box: <?php echo $randomNumber; ?> </td> </tr> <tr> <td><div align="right"> <input type="image" src="images/blue_submit_button.jpg" value="Submit" alt="Submit"> </div></td> </tr> </table> <input type="hidden" id="check" name="check" value="<?php echo $randomNumber;?>" /> </form> </div> <!-- /quick_contact_form --> As always, any help is much appreciated! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/195560-headers-sent-issue-on-include-file/ Share on other sites More sharing options...
trq Posted March 17, 2010 Share Posted March 17, 2010 Whether or not a file is an include is irrelevant. You are aware however you cannot send ANY output to a browser prior to calling header() ? Quote Link to comment https://forums.phpfreaks.com/topic/195560-headers-sent-issue-on-include-file/#findComment-1027561 Share on other sites More sharing options...
PFMaBiSmAd Posted March 17, 2010 Share Posted March 17, 2010 Read the error message. It tells you where the output is occurring at that is preventing the header() from working. You must find and fix whatever is causing the output. Quote Link to comment https://forums.phpfreaks.com/topic/195560-headers-sent-issue-on-include-file/#findComment-1027567 Share on other sites More sharing options...
jarvis Posted March 17, 2010 Author Share Posted March 17, 2010 Thanks for the replies, the error says: Warning: Cannot modify header information - headers already sent by (output started at \inc\header.php:139) in \inc\quick_enquiry_form2.php on line 44 Line 139 in header.php is the navigation and line 44 of quick_enquiry_form2.php has no spacing and looks like [php $mailResult = @mail($to, $subject, $message, $headers); if (!$mailResult) { die("Mail Error: $php_errormsg"); } header("Location:thanks.php");[/code] There are also no spaces at the top of that file. Sorry, someone is gonna have to speak & spell it to me! :-( Quote Link to comment https://forums.phpfreaks.com/topic/195560-headers-sent-issue-on-include-file/#findComment-1027574 Share on other sites More sharing options...
trq Posted March 17, 2010 Share Posted March 17, 2010 output started at \inc\header.php:139 Find the output and remove / prevent it from being sent. Quote Link to comment https://forums.phpfreaks.com/topic/195560-headers-sent-issue-on-include-file/#findComment-1027575 Share on other sites More sharing options...
jarvis Posted March 17, 2010 Author Share Posted March 17, 2010 Like finding a needle in a haystack! I cannot see anywhere what could be causing this! I've an index.php which calls in header.php and quick_enquiry_form2.php Do I need to remove all spacing? Would it help if I posted the code? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/195560-headers-sent-issue-on-include-file/#findComment-1027583 Share on other sites More sharing options...
PFMaBiSmAd Posted March 17, 2010 Share Posted March 17, 2010 Why are you (attempting) to output your page contents up to the point of the header() statement, then redirect to a different page? If you could do this, it would waste your hosting bandwidth on every page request and waste the processing time it takes to generate and output that content, so you are fortunate that it does not work, your site would operate poorly. Why not just output the thank you content on the page you are already on? A header() redirect tells the browser to request the target page, the same as if you browsed to it. That in itself takes extra time. Quote Link to comment https://forums.phpfreaks.com/topic/195560-headers-sent-issue-on-include-file/#findComment-1027587 Share on other sites More sharing options...
PFMaBiSmAd Posted March 17, 2010 Share Posted March 17, 2010 You already found what the output is - Line 139 in header.php is the navigation Quote Link to comment https://forums.phpfreaks.com/topic/195560-headers-sent-issue-on-include-file/#findComment-1027588 Share on other sites More sharing options...
jarvis Posted March 17, 2010 Author Share Posted March 17, 2010 Hi PFMaBiSmAd, Because of the way the site was constructed, I thought this would be the best way. The structure is: Header Page name, for example, index, about, thanks etc (which has several includes 1 being the enquiry form) Footer The enquiry form says thank you once you submit it, however, it would be better to fill in the form and send them to the thanks page. I thought header() was the only option to do this. If there is another way, I'm all ears! However, as this is a live site, I don't want to recode pages Quote Link to comment https://forums.phpfreaks.com/topic/195560-headers-sent-issue-on-include-file/#findComment-1027590 Share on other sites More sharing options...
jarvis Posted March 18, 2010 Author Share Posted March 18, 2010 Solved used redirectBrowserToURL("http://www.domain.com/thanks.php"); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/195560-headers-sent-issue-on-include-file/#findComment-1027979 Share on other sites More sharing options...
trq Posted March 18, 2010 Share Posted March 18, 2010 Probably some javascript, meta tag or output buffering hack? Quote Link to comment https://forums.phpfreaks.com/topic/195560-headers-sent-issue-on-include-file/#findComment-1027995 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.