IamSuchANoob Posted February 26, 2015 Share Posted February 26, 2015 Hey! I have a problem, I have a PHP script which sends mail from a html form. Weird is, it works great on another host. error: Warning: Cannot modify header information - headers already sent by (output started at /home/sbsbitum/public_html/process2.php:5) in /home/sbsbitum/public_html/process2.php on line 60 php: <?php if(!isset($_POST['submit'])) { //This page should not be accessed directly. Need to submit the form. echo "error; you need to submit the form!"; } $name = $_POST['cformname']; $email = $_POST['cformemail']; $message = $_POST['cformmessage']; //Validate first if(empty($name)||empty($email)) { echo "Name and email are mandatory!"; exit; } if(IsInjected($email)) { echo "Bad email value!"; exit; } $to = 'info@test.com';// to email address // subject $subject = 'Message from homepage.'; // message $body = ' <html> <head> <title>Viesti</title> </head> <body> <img src=""> <table rules="all" style="border-color: #666;" cellpadding="10"> <tr style="background:" #eee;><td><strong>Nimi:</strong></td><td>'.$name.'</td></tr> <tr><td<strong>Email:</td<strong><td>'.$email.'</td></tr> <tr><td<strong>Viesti:</td<strong><td>'.$message.'</td></tr> </table> </body> </html> '; $headers .='MIME-Version: 1.0' . "\r\n"; $headers .='Content-type: text/html; charset=utf-8' . "\r\n"; // Additional headers $headers .= 'From: sender '.$email.' ' . "\r\n"; // Mail it mail($to, $subject, $body, $headers); //done. redirect to thank-you page. header('Location: lehed/thx.php'); // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } function filter($data) { $data = trim(htmlentities(strip_tags($data))); if (get_magic_quotes_gpc()) { $data = stripslashes($data); } return $data; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/294909-headers-problem/ Share on other sites More sharing options...
requinix Posted February 26, 2015 Share Posted February 26, 2015 Can't use header() if there's been any output. The error message tells you where the output happened. You have to decide whether that output should be moved (and if so, where to), or whether there's a bug and your code wasn't supposed to header() at all, or something else. Quote Link to comment https://forums.phpfreaks.com/topic/294909-headers-problem/#findComment-1506792 Share on other sites More sharing options...
IamSuchANoob Posted February 26, 2015 Author Share Posted February 26, 2015 Yeah, I try to send the user after sending in the form to the page thx.php (just image). I tried the ob_start(); solution but I don't like workarounds. (for example, with ob_start(); the HTML doesn't show up properly for example in RoundCube client). Quote Link to comment https://forums.phpfreaks.com/topic/294909-headers-problem/#findComment-1506794 Share on other sites More sharing options...
Ch0cu3r Posted February 26, 2015 Share Posted February 26, 2015 Then use header() before any output. Output is considered to be anything that is being echo/print'd or anything that is outside of the <?php ?> tags. The error tells where output was detected Warning: Cannot modify header information - headers already sent by (output started at /home/sbsbitum/public_html/process2.php:5) in /home/sbsbitum/public_html/process2.php on line 60 In red it tells you the location of the file and line number on which the output started. In blue it tells you the location and line number where you attempted to use header(). So what is the first 5 lines of process2.php? Quote Link to comment https://forums.phpfreaks.com/topic/294909-headers-problem/#findComment-1506804 Share on other sites More sharing options...
IamSuchANoob Posted February 26, 2015 Author Share Posted February 26, 2015 //This page should not be accessed directly. Need to submit the form. echo "error; you need to submit the form!"; Quote Link to comment https://forums.phpfreaks.com/topic/294909-headers-problem/#findComment-1506811 Share on other sites More sharing options...
cyberRobot Posted February 26, 2015 Share Posted February 26, 2015 Try adding an exit statement after the error message. //This page should not be accessed directly. Need to submit the form. echo "error; you need to submit the form!"; exit; The rest of the script seems to be geared toward processing a form submission. So you don't want that code being executed if the page was accessed directly (without submitting a form). Quote Link to comment https://forums.phpfreaks.com/topic/294909-headers-problem/#findComment-1506812 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.