Jump to content

Problem modifying headers + other strange problems


Recommended Posts

Right, this is odd.  Help would be greatly appreciated.

 

I'm developing a website (http://174.132.115.253/~mohawks/signup.php) and a signup form.  I have the same version of the website A) on my local machine and B) on some webspace.  The link above is on the webspace.

 

On the webspace, reguardless what fields i fill in, when the page reloads after pressing submit I get the good old "Warning: Cannot modify header information - headers already sent by (output started at /home/mohawks/public_html/global_app.php:10) in /home/mohawks/public_html/signup.php on line 41" error message.  I've made sure all header outputs are before the <HTML> tag and i've removed any stray spaces or returns in my editor.

 

Line 10 within global_app.php has my doctype

 

echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';

 

If I remove this line everything works great!, but of course i need my doctype and regardless where I put it the error persists.

 

--

 

Now, the localhost version of the same form also doesn't work but in a different way.

 

When the submit button is pressed firefox pauses to think for a second then offers this error message: "The connection was reset. The connection to the server was reset while the page was loading." (same in ie and chrome with less hopeful error msgs).  This differs from the webspace version in that when I remove the doctype nothing changes.  However I can remove a function call which does remove the error.

 

include 'include/functions/send_email.php'; <-- this i can edit out and it works fine

 

<?php

function send_email($f_name='',$l_name='',$to='',$from='',$subject='',$body='')

{

$headers  = "MIME-Version: 1.0\r\n";

$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

$headers .= "To: ".$f_name." ".$l_name." <".$to.">\r\n";

$headers .= "From: ".$from."\r\n";

 

if(mail($to, $subject, $body, $headers)) { //sends email (as per settings in php.ini) and returns true if successful

return true;

}

return false;

}

?>

 

So there you go.. crazy stuff!  I'm pretty sure they are the same error but because the php versions on the localhost (v5.3.0) and webspace (v5.2.9) are different they are being handled in different ways.

 

Any thoughts anyone?  or anymore info you need?

 

ta!

Without seeing your page that has the DOCTYPE in it, it would be hard to say what is causing the error.

 

If your localhost server doesn't have an email MTA installed, you might be getting your error because the server doesn't know what to do. I get a different error on my localhost when I try to send mail, but an error is an error.

You're welcome to see more of my code.

 

signup.php is what the browser calls first:

 

<?php

include("global_app.php");

 

//I've omitted irrelevant code here

 

if(isset($_POST['submit_check'])) {      //if user has pressed submit

  //I've omitted irrelevant code here - code that puts user inputs into variables+sessions

 

        //I've omitted more code here - code that puts a list of errors into $error

 

  if(isset($error)) {    //if there is an error, reload page. Code elsewhere will pick them up and ask user to try again

      header("Location: " . $_SERVER['PHP_SELF'] . "?" . $error);

}

 

  //I've omitted irrelevant code here - composes email into $first_name, $last_name, $email, $from, $subject and $body.

 

  include 'include/functions/send_email.php';

  if(send_email($first_name,$last_name,$email,$from,$subject,$body)) {      //calls function that sends the email

      echo $confirmation_text;

  } else {

      echo 'There was an error sending the email';

  }

}

?>

<html>

<head><?php include 'head.php'; ?></head>

<body>

  <?php include 'header.php'; ?>

  <div id="background_main"></div>

  <div id="clipboard">

      <?php include 'clipboard_top.php'; ?>

      <div id="container">

        <div id="container_left"></div>

        <div id="container_right"></div>

        <div id="container_center">

            <?php include("pages/template.php"); ?>

        </div>

        <div id="container_ussu"><img src="images/ussu_frisbee.jpg"></img></div>

      </div>

      <div id="hand"><img src="images/hand.png"></img></div>

      <div id="bottom"></div>

  </div>

  <div id="footer"><img src="images/feet.png"></img></div>

</body>

</html>

 

You can see the very first line calls global_app.php which includes my DOCTYPE:

 

<?php

include("include/sys_constants.php");

include("include/functions/db_connect.php");

db_connect($DB_NAME);

session_start();

 

$path_parts = pathinfo($_SERVER['PHP_SELF']);

$active_page = $path_parts['filename'];

 

echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';

//I've omitted irrelevant code here

include("pages/content/" . $active_page . ".php");?>

 

original files attached

 

As for my localhost.  It does have an SMTP server installed but its cheap (as in free) and nasty. It seems to be sending emails mind you. But you're right, an errors an error.  Needs fixing :s

 

 

[attachment deleted by admin]

Yeah i tried that.. same problem.  I even get a brand new error

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/mohawks/public_html/signup.php:2) in /home/mohawks/public_html/global_app.php on line 5

 

So I move it back to after the session handling

Ok, your problem is the header() that is trying to redirect the user, but after the DOCTYPE is output. The DOCTYPE, and any other output to the browser, needs to be AFTER the header redirect. If you were redirecting, you wouldn't need the DOCTYPE, ya know what I'm saying? This is the modification to the header that php is complaining about.

 

You should store the output in a variable, and echo (if necessary) AFTER all header modification and session start.

As sKunKbad said the problem is that you have output before you call header() functions. Also you have commented a lot of code out because of errors, you should try to fix all errors and warnings and even notices if possible. There is always a way to fix them.

Cool thanks everyone.  I'm a bit annoyed with myself because this is a problem I read in many places.  Since the doctype begins at the start of any html page I wanted to print it out from a file that gets read at the beginning of all the website's pages (global_app). I didn't realise the echo function tells the php the headers are now finished. now i do :)

 

thanks again!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.