Support » Plugins » header(“Location: $PHP_SELF”) problem

  • I’m using a script that enables users to set their own accesskeys but I’m getting a Cannot modify header information – headers already sent by error which seems to be caused by header(“Location: $PHP_SELF”) in the script. Is there any way around this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • This link for referencing headers already sent in WP. FAQ_Troubleshooting#How_do_I_solve_the_Headers_already_sent_warning_problem
    You mention a script though, I’m assuming that’s outside of WP?

    Yeah, I saw that, but it seems to refer to problems caused by WordPress files whereas mine, although it resides in my theme folder, is not a WP script. I don’t know much about how headers work in PHP so was wondering if there’s some kinda conflict.

    Not really. Something (output) is being sent to the browser before the headers are being sent, and like that page suggested, it could be anything from white space to an errant tag.

    This is a common problem that people make when working with PHP’s header() function. In a nutshell what the error is telling you, is that you can not redirect because your PHP script has already sent HTML to the browser.

    The long explanation is this: When a browser request your script from the server, it sends the request in a header, like this:

    GET /index.php HTTP/1.1

    And the server responds with something like this:

    HTTP/1.1 200 OK
    Content-Type: text/html; charset=UTF-8

    Followed by the document’s HTML. Once the HTML has started flowing to the browser, *no more headers* can be sent. Now PHP’s header() function simply does this:

    HTTP/1.1 302 OK

    When the browser gets this response, it redirects to the “location” URL instead. PHP’s header() function simply inserts “Location:” into the response header. But like I said above, once HTML has started flowing to the browser, you can’t send any more headers.

    Use of the header() function has to be before *anything* is output in your script. No text, no HTML, not even a single space.

    – Sean

    I wasn’t looking at the example properly which had the header(“Location: $PHP_SELF”) above the doctype. :/ So I tried splitting the script up with this in my header above the doctype:


    $arBindings = array("Home"=>array("/index.php", "1"),
    "Search Criteria"=>array("searchcriteria", "4"),
    "Quality Assurance"=>"/services.php",
    "Articles Archive"=>"/articles.php");

    $objAK = new AccessKeys($arBindings);

    if ($_POST)
    if ($_POST["default"])
    header("Location: $PHP_SELF");
    else if ($objAK->setCookies())
    header("Location: $PHP_SELF");

    and the call that generates the output in my page, but I get a Call to a member function displayOptions() on a non-object error message.
    Trying another script that does the same thing works fine if I create an example outside of WP, but if I try to do it within WP, I get Invalid argument supplied for foreach(). The foreach is working on an array set up inside an include inserted in the header above the doctype etc.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘header(“Location: $PHP_SELF”) problem’ is closed to new replies.