Results 1 to 4 of 4

Thread: WARNING: Cannot modify header information

  1. #1
    Join Date
    Sep 2008
    Posts
    36
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default WARNING: Cannot modify header information

    Hi Guys,

    I am hosting a second website on my host and I just set it up and everything is fine except for a php script that I have in there which brings me the error below instead of redirecting to the location specified in the header.

    Warning: Cannot modify header information - headers already sent by (output started at /home/content/myaccount/html/myfolder/write.php:1) in /home/content/myaccount/html/myfolder/write.php on line 3

    I have checked and there is no "white space" or anything. As a matter of fact the script works fine on a different domain hosted elsewhere.

    Not sure if you need more info, but can you please help?

    Thanks,

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    It is odd that it is different on different hosts, but there must be some output somewhere. One possibility is whitespace (but that should be consistent on different hosts). Another is that there is an error, and that error only occurs on one host.

    To actually debug this we'd need to see the entire script, in fact all of your PHP pages that are included in the execution of the page. Remember that the error may be on a different page if you're including any others or if this is not the main page (but is included into the main page).
    But that might be a lot of work depending on how many pages you have and how long they are.

    The solution here is to go through step by step in the script and find what might be the problem. Comment out sections as needed to figure out where the problem is, then fix it.

    Additionally, read the error carefully: it claims that the problem is on write.php on line 1. What is that line? Are you sure there's no whitespace or anything like that?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Sep 2008
    Posts
    36
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default

    Hey,

    Thanks for replying. It's a very straight and simple script:

    PHP Code:
    <?php
    mail
    "me@myemail.com""Email Subject""email body""From: sender@email.com" );
    header("Location: http://www.targetsite.com/");
    $handle fopen("data.txt""a");
    foreach(
    $_GET as $variable => $value)
    {
    fwrite($handle$variable);
    fwrite($handle"=");
    fwrite($handle$value);
    fwrite($handle"\r\n");
    }
    exit;
    ?>
    I even tried to delete that line before the header, or put it after, but it didn't make a difference. And as I said, it works just like this on another host.

    Thanks,

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    That is strange. Here are some thoughts, including some basic points you've probably tried, but I don't see anything else.

    1. Be sure that there are no invisible characters in that. Maybe an invisible spacing character of some sort is before the <?php. So just retype those lines to make sure that cutting and pasting didn't make something strange happen.
    (If you're interested in the technical details, then you could make the entire script into a string-- put it in quotes-- then ask PHP to tell you how many characters are in it and see if that doesn't match the number you can count/see.)

    2. Does the mail function work? It's VERY likely that you have the mail server on the new server configured incorrectly, since this can vary by host. Since it looks like it's around that line, I am guessing it might have something to do with that. The code is right, and if your old server has a properly configured mail server it would work, so if the new one does not have a properly configured mail server, it will by mysteriously broken.
    If you delete the mail() line, OR if you move it to below the header, does the script still not work? I think you said that, but I want to confirm it.
    Secondly, if you delete everything else and leave JUST the mail() line, does that work? Do you see any errors?

    3. Verify that error reporting is on including all warnings, etc. Then see if anything comes up. There might be a parse error (I don't see one) or something else that could be causing an error message before the header. I'm not sure if that makes any sense (if it's not being output it shouldn't mess with the headers), but that's one possibility.

    Some errors show up in PHP because of configuration issues. For example, in the newer version (5.3+, I think), it will yell at you if you haven't configured a time zone for the server. It's annoying, but something like that might be the issue.

    4. Are you accessing this script directly or via include() in another script? Try access it directly to see if that solves it. If not, then the problem is definitely in this script. Just eliminating things...



    Finally, there's an organization issue with your script: I'm not positive that code after the header() will execute. If the browser is redirected immediately, then the server might not have time to finish the code after it. Usually, I think it will execute, but if the redirect goes extremely fast and the server notices the user is no longer receiving data, then it will abort the process.
    Of course you can't have any OUTPUT before the header, but you can do file manipulation stuff like you have below it. That's fine.

    You also don't need "exit" at the end-- that's meant to stop a script before the end. It's implied at the end (where you have it).

    Finally, there's a slight security risk because you're using $_GET without checking what might be sent. Hackers could send whatever values they want. If you are ONLY writing this to a file, there's no real danger. But if you ever want to use that later, just be sure you validate the data.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •