WordPress.org

Ready to get started?Download WordPress

Forums

header("Location: $PHP_SELF") problem (6 posts)

  1. Tyssen
    Member
    Posted 8 years ago #

    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?

  2. Michael Bishop

    Posted 8 years ago #

    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?

  3. Tyssen
    Member
    Posted 8 years ago #

    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.

  4. IIIIIIIV
    Member
    Posted 8 years ago #

    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.

  5. forceagainstsomething
    Member
    Posted 8 years ago #

    Tyssen,
    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
    Host: http://www.yoursite.com

    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
    Location: http://www.myothersite.com

    When the browser gets this response, it redirects to the "location" URL instead. PHP's header() function simply inserts "Location: http://www.myothersite.com" 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

  6. Tyssen
    Member
    Posted 8 years ago #

    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:


    require_once("ak.class.php");

    $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"])
    {
    $objAK->useDefaults();
    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.