making bbPress (and WordPress) work better!

$_SERVER['REQUEST_URI'] doesn’t always work correctly! Here’s how to fix it.

Here’s something important for WP plugin developers that I have discovered on my server with wordpress and many plugins.

While PHP programmers have been taught to use
$_SERVER['REQUEST_URI']
to fetch the current page URI, IT DOESN’T WORK CORRECTLY in all cases with wordpress’s rewrite functions.

I discovered this while working with the paged-comments plugin. It caused the template and other plugins to not see the proper URI and miss out on the extra stub in the URL.

After some studying of phpinfo results, I discovered that the proper, complete URI was shown in:
$GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'];

Yes, I know they should be identical but apparently it’s an Apache + PHP bug in some environments.

My server has apache 1.33 and php 4.2 so your milage may vary on the bug.
But since ALOT of people have that server combo, trust me, this prevents problems.

14 responses

  1. Rodrigo

    I had a similar problem. My php file inserted the $_SERVER['REQUEST_URI'] bit in a form sending stuff using the post method, and sent it as a hidden field to the logout script.

    The logout script would get it and then re-direct to an index page using the header() function. However, this redirection only worked with Mac’s Safari for some weird reason. Anyway, the solution for me was to break out of PHP and use html’s tag. This way it all worked!. It’s strange how this works on some browsers and on others it doesn’t since it seems to be a server side issue not a client-side issue. Anyway, I’m happy I fixed it!

    February 12, 2008 at 11:03 pm

    • THis is ComMEnt for this page not reply. Please learn more before you try to make some new exploration..

      April 15, 2011 at 12:48 pm

  2. Hi,
    Do you how to get this technique working on bbpress?

    The problem is tracking conversions, as the “register” button is on every page, conversions cant be measured accurately if users register to the website, clicking on the button from a different page.

    If I have domain/
    And the register button is on the index domain/register.php
    Measuring conversions that went from the index would go fine.

    Suppose if a user is on page:
    Domain/topic.php?id=694

    I’d really love this to change into: domain/register.php?topic.php?id=694 (or something similar, but the entrance path should be also in the url)

    Any ideas?

    Thanks.

    July 26, 2009 at 2:10 pm

  3. I’ve been banging my head on the monitor for the last couple of hours trying to get a conditional to work. Your article has finally got me going!

    Thank you so very much for sharing!

    Regards from Brazil,

    Alexandre

    December 7, 2009 at 4:23 pm

  4. ixi

    thank you thank you! i was just very frustrated about the same issue. now solved peacefully.

    all the best
    ixi

    December 14, 2009 at 4:00 pm

  5. Thanks a lot! It was very useful ;)

    July 30, 2010 at 9:05 am

  6. Pingback: Ticket #15209 ($_SERVER['REQUEST_URI'] Doesn’t always work correctly) created | Wordpress Services

  7. :– I am really thankful to this topic because it really gives up to date information ~,`

    January 28, 2011 at 1:23 pm

  8. to own absolutely data d fabric graphics for that url that you are constructing.

    April 27, 2011 at 8:54 am

  9. We have read some terrific things here. Worth book-marking for revisiting. I wonder what amount effort you put to produce this type of impressive informative web page.

    September 15, 2011 at 1:32 am

  10. Martin

    Hi, I have a problem more complex, if I have the following code with the url:
    http://domain.com/?seccion=articulos&para=Caballeros

    function PageURL(){
    $pageURL = ‘http';
    if($_SERVER["HTTPS"] == “on”){$pageURL .= “s”;}
    $pageURL .= “://”;
    if ($_SERVER["SERVER_PORT"] != “80”){
    $pageURL .= $_SERVER["SERVER_NAME"].”:”.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    }else{
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
    }

    echo $url=PageURL();

    RUSULT = http://localhost/cuteberryfashion/?seccion=articulos¶=Caballeros

    why ¶?, where is the variable $_GET[para]

    January 24, 2012 at 11:35 pm

  11. Ben

    @martin

    Not sure what’s causing your error, but you could do it another way:

    $q = $_SERVER['QUERY_STRING'] ;
    parse_str($q,$vars);
    print_r($vars);

    That would return an array like so:

    Array
    (
    [seccion] => articulos
    [para] => Caballeros
    )

    Once you have the information, you can re-build the string, however you see fit.

    February 2, 2012 at 9:09 pm

  12. Debashis Chowdhury

    Hi,
    I am also having an issue with $_SERVER[REQUEST_URI] in my ZEND framework based project. I tried to follow your solution. But, according to php manual (http://php.net/manual/en/reserved.variables.server.php), HTTP_SERVER_VARS is deprecated.

    Please suggest how can i get the exact path?

    April 11, 2013 at 6:01 am

    • Well hopefully you aren’t using PHP 4.2 anymore. So yes my method here is deprecated.

      Getting the wrong result for REQUEST_URI is a symptom of a buggy environment. It may be fixable by upgrading various parts of your server software, even a minor update may fix it.

      What I would do in your case if that is not possible is to study the results of PHPINFO and see if any variable DOES contain the proper path you are looking for.

      April 11, 2013 at 9:48 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 45 other followers