Dear all,

When I tried to diverted the form to thank you page:
header( 'Location: http://www.iklaneka.net/directories/thanks-u.php' );

It came out error:
Warning: Cannot modify header information - headers already sent by (output started at /home/iklaneka/public_html/directories/contact-us.php:163) in /home/iklaneka/public_html/directories/contact-us.php on line 253

Anybody can find what's wrong. Thank you... =Radz=i


<html>
<head>
<title>Iklaneka.Net</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>
	<div id="site">
		<div id="wrapper">
			<div id="header" style="width: 1000px; height: 415px">
				<a href="index.html" id="logo"><img src="images/spacer.gif" alt="" width="360" height="90" /></a>																			
			<ul id="menu">
					<li><a class="but1" href="index.html">Home</a>
					<li><a class="but2" href="bizdir-insert.html">Submit Directory</a></a></li>
				</ul>
				<div class="logo">Your One Stop Business Sourcing Network.</div>
				<div class="rss"><a href="#"><img src="images/spacer.gif" alt="" width="27" height="62" /></a></div>
				<div style="position: absolute; width: 599px; height: 38px; z-index: 1; left: 733px; top: 41px" id="layer1">

<form action="http://www.google.com.my" id="cse-search-box" target="_blank">
  <div>
    <input type="hidden" name="cx" value="partner-pub-0611092848813518:6550017373" />
    <input type="hidden" name="ie" value="UTF-8" />
    <input type="text" name="q" size="55" />
    <input type="submit" name="sa" value="Search" />
  </div>
</form>

<script type="text/javascript" src="http://www.google.com/cse/query_renderer.js"></script>
<div id="queries"></div>
<script src="http://www.google.com/cse/api/partner-pub-0611092848813518/cse/6550017373/queries/js?oe=UTF-8&amp;callback=(new+PopularQueryRenderer(document.getElementById(%22queries%22))).render"></script>
</div>
&nbsp;</div>
			<div id="content">
				<div id="sidebar" style="width: 190px; height: 982px">
					<h4>Directories</h4>
					<ul>
						<li></li>
						<li>
						<a target="_blank" href="bizdir/advertise-list_records.php">Accounting</a></li>
						<li>
                                                <a target="_blank" href="bizdir/advertise-list_records.php">Browse Directory</a></li>
						<li></li>
					</ul>
					<a href="#" class="more">more resourses</a><br />
				</div>
				<div id="main" style="width: 702px; height: 1020px">
					<h4>Send Email to Iklaneka.Net admin</h4>
					</h4>

				<div style="position: absolute; width: 620px; height: 611px; z-index: 2; left: 600px; top: 495px" id="layer2"><?php 
// Pear library includes
include_once('Mail.php');
include_once('/home/iklaneka/php/Mail/mime.php'); 

//Settings 
$max_allowed_file_size = 5000; // size in KB 
$upload_folder = '/home/iklaneka//public_html/temp/'; //<-- this folder must be writeable by the script
$your_email = $_REQUEST['dept_email'];//<<--  update this to your email address

$errors ='';

if(isset($_POST['submit']))
{

$name_of_uploaded_file =  basename($_FILES['uploaded_file']['name']);


$type_of_uploaded_file = substr($name_of_uploaded_file, 
						strrpos($name_of_uploaded_file, '.') + 1);

$size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;

if(empty($_POST['name'])||empty($_POST['email']))
{
	$errors .= "\n Name and Email are required fields. ";	
}
if(IsInjected($visitor_email))
{
	$errors .= "\n Bad email value!";
}

if($size_of_uploaded_file > $max_allowed_file_size ) 
{
	$errors .= "\n Size of file should be less than $max_allowed_file_size";
}
if(empty($errors))
{
	//copy the temp. uploaded file to uploads folder
	$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
	$tmp_path = $_FILES["uploaded_file"]["tmp_name"];

	if(is_uploaded_file($tmp_path))
	{
	    if(!copy($tmp_path,$path_of_uploaded_file))
	    {
	    	$errors .= '\n error while copying the uploaded file';
	    }
	}

	//send the email

  $email = $_REQUEST['email'] ;
  $message = $_REQUEST['message'] ;
  $dept_email = $_REQUEST['dept_email'] ;

  mail( "$email", "Message to Iklaneka.Net",
    $message="Thank you for contacting Iklaneka.Net. Your message has been submitted to our action.", "From: $dept_email" );

	$name = $_POST['name'];
	$visitor_email = $_POST['email'];
	$user_message = $_POST['message'];
	$to = $your_email;
	$subject="Message from Iklaneka.Net Customer";
	$from = $visitor_email;
	$text = "A user  $name has sent you this message:\n $user_message";

	$message = new Mail_mime(); 
	$message->setTXTBody($text); 
	$message->addAttachment($path_of_uploaded_file);
	$body = $message->get();
	$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
	$headers = $message->headers($extraheaders);
	$mail = Mail::factory("mail");
	$mail->send($to, $headers, $body);

	//redirect to 'thank-you page
	header( 'Location: http://www.iklaneka.net/directories/thanks-u.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;
  }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 

<html>
<head>
	<title>Email Form With File Attachment</title>
<!-- define some style elements-->
<style>
label,a, body 
{
	font-family : Arial, Helvetica, sans-serif;
	font-size : 12px; 
}

</style>	
<!-- a helper script for vaidating the form-->
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>	
</head>
<?php
if(!empty($errors))
{
	echo nl2br($errors);
}

?>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator  = new Validator("email_form_with_php");
frmvalidator.addValidation("name","req","Please provide your name"); 
frmvalidator.addValidation("email","req","Please provide your email"); 
frmvalidator.addValidation("email","email","Please enter a valid email address"); 
</script>

<p><noscript><small><a href='http://www.html-form-guide.com/email-form/php-email-form-attachment.html'
How to attach file to email in PHP</a> article page.</small>
</p>
</noscript><table border="1" width="48%">
	<tr>
		<td bgcolor="#FAFAD2">
<form method="POST" name="email_form_with_php" 
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data"> 
<p style="margin-left: 30px; margin-top:0; margin-bottom:0">
&nbsp;</p>
<p style="margin-left: 30px; margin-top:0; margin-bottom:0">
<label for='name' style="font-family: Arial, Helvetica, sans-serif; font-size: 12px">
Your Name: </label><br>
<input type="text" name="name" size="25" >
</p>
<p style="margin-left: 30px; margin-top:0; margin-bottom:0">
&nbsp;</p>
<p style="margin-left: 30px; margin-top:0; margin-bottom:0">
<label for='email' style="font-family: Arial, Helvetica, sans-serif; font-size: 12px">
Your Email: </label><br>
<input type="text" name="email" size="25" >
</p>
<p style="margin-left: 30px; margin-top:0; margin-bottom:0">
&nbsp;</p>
<p style="margin-left: 30px; margin-top:0; margin-bottom:0">
<font style="font-size: 9pt">To :&nbsp;</font></p>
<p style="margin-left: 30px; margin-top:0; margin-bottom:0"><select name="dept_email"
 style="font-size: 9pt;">
                          <option selected="selected" dept_email="admin@iklaneka.net">admin@iklaneka.net</option>
                          <option dept_email="muhammad.radzi@gmail.com">muhammad.radzi@gmail.com</option>
                          </select>
</p>
<p style="margin-left: 30px; margin-top:0; margin-bottom:0">
<p style="margin-left: 30px; margin-top:0; margin-bottom:0">
&nbsp;</p>
<label for='message' style="font-family: Arial, Helvetica, sans-serif; font-size: 12px"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Your Message:</label> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<textarea name="message" rows="21" cols="70"></textarea>
</p>
<p style="margin-left: 30px; margin-top:0; margin-bottom:0">
<p style="margin-left: 30px; margin-top:0; margin-bottom:0">
&nbsp;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<label for='uploaded_file' style="font-family: Arial, Helvetica, sans-serif; font-size: 12px">Select A File To Attach:</label> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="file" name="uploaded_file" size="21">&nbsp;&nbsp;
</p>
<p style="margin-left: 30px; margin-top:0; margin-bottom:0">
&nbsp;</p>
<p style="margin-left: 30px"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input type="submit" value="Submit" name='submit'>
</p>
</form>
		<p>&nbsp;</td>
	</tr>
</table>

</body>
</html></div>
					<p class="pad15">&nbsp;</p>
				</div>
			</div>
			<div id="footer">
				<ul>
					<li><a href="index.html">Home</a> |</li>
				</ul>
			</div>
		</div>
	</div>

<a id="mws4921273" href="http://webstats.motigo.com/">
<img width="80" height="15" border="0" alt="Free counter and web stats" src="http://m1.webstats.motigo.com/n80x15.gif?id=AEsXuQFm/EPwNpZUFH2hbYUG/4kw" /></a>
<script src="http://m1.webstats.motigo.com/c.js?id=4921273&amp;lang=EN&amp;i=3" type="text/javascript"></script>

</body>
</html>

    You can't use [man]header[/man] after starting on the content of the page (in your case this happened on line 163 of contact-us.php).

      Weedpacket;11024605 wrote:

      You can't use [man]header[/man] after starting on the content of the page (in your case this happened on line 163 of contact-us.php).

      Yes, when I remove header, the error gone... but what is the command to use for me to divert the page to thanks-u.php page. TQ 🙂

        The "command" isn't the problem. The problem is with the structure of your code.

        You output a bunch of data, and then check to see if you might want to redirect the user elsewhere via header(). Since you can't call header() after sending any output, the solution should be apparent: Move all of the code that may result in the call to header() such that it comes before any output.

          5 days later

          Another quick fix to this that I found a couple of years ago is to use ob_start(); and ob_end_flush();.

          Example:

          ob_start();
          
          echo 'some content';
          header('Location:./somepage.php');
          
          ob_end_flush();[code=php]
          
          This will allow you to put a header(); within the page. In a nutshell, the ob_start(); function just tells the PHP server to wait for the ob_start_end(); function before sending any output. I think.

            Sorry. Missed out the end tag for PHP.

            ob_start();
            
            echo 'some content';
            header('Location:./somepage.php');
            
            ob_end_flush();

            I should also mention that this is only a quick fix. Try and avoid using this as a good coding principle.

              BradGhost;11024825 wrote:

              I should also mention that this is only a quick fix. Try and avoid using this as a good coding principle.

              That last bit is exactly why I've never called that approach a "fix" but more of a "sloppy workaround" since you're really just telling PHP to utilize extra server resources to make up for your lack of a more intelligent code design.

              Also, don't forget that the 'Location' header requires a full URI. :p

                Write a Reply...