Fixing IIS & PHP Variables – Set $_SERVER[‘REQUEST_URI’] Yourself

By  on  

About 8 months into my programming career I needed to code a content management system (CMS). This was quite a challenge so early into my professional journey but I was up to it. The front-end was the easiest part of the project -- a lot quick SQL queries for bringing intros and conclusions to pages that needed to be static due to PHP programming, a sub-navigation builder (the menu was stored in the DB), and the usual take-forever forms.

The most time-consuming part of the project was creating an administrative panel. The administrative panel needed to be highly flexible and portable so that it could be used on other projects. In the end, the framework for the administrative panel has been perfect for all projects since that one. We keep adding modules which save time and allow time for more front-end programming.

With that particular project, however, we ran into a problem that we didn't foresee. -- the customer informed us that we needed to host the website on an IIS server. This was not great news but we knew that IIS would support PHP so we didn't panic. The big problem I ran into is that IIS does not support PHP's $_SERVER['REQUEST_URI'] variable and worse is that I didn't know this until it was time to launch.

I used the $_SERVER['REQUEST_URI'] all over the administration panel. Every "page" of the website ran through the index.php to give me MVC funtionality. Doing so made me need to use the self-referencing $_SERVER['REQUEST_URI'] variable because it provided me the all-important querystring variables I needed to keep my place in the file.

How'd I fix the problem? It was actually quite simple:

if (!isset($_SERVER['REQUEST_URI']))
{
       $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
       if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING']; }
}

This probably seems easy and obvious to most but at the time I was quite new. Hopefully this can help someone save a lot of time!

Recent Features

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

  • By
    Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

Incredible Demos

  • By
    Send Email Notifications for Broken Images Using MooTools AJAX

    One of the little known JavaScript events is the image onError event. This event is triggered when an image 404's out because it doesn't exist. Broken images can make your website look unprofessional and it's important to fix broken images as soon as possible.

  • By
    MooTools dwCheckboxes Plugin

    Update / Fix: The checkboxes will no longer toggle when the "mouseup" event doesn't occur on a checkbox. Every morning I wake up to a bunch of emails in my Gmail inbox that I delete without reading. I end up clicking so many damn checkboxes...

Discussion

  1. sometimes web server set a querystring but is empty so I suggest to modify the code like this:

    if (!isset($_SERVER['REQUEST_URI']))  {
    
            $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
    
            if (isset($_SERVER['QUERY_STRING']) AND $_SERVER['QUERY_STRING'] != "") {
            	$_SERVER['REQUEST_URI'] .= '?'.$_SERVER['QUERY_STRING'];
            }
    }
  2. Dmitry-Sh

    Thank you, it works.

  3. Thanks – it saved me a bundle of time.
    BTW, I changed line 3 to

           $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],0 );
    

    That way the leading “/” wasn’t dropped out.

    David IB

  4. Elias

    Thank you, the article matched exactly what i passed through (modular cms control panel, client with iis, etc), and you provided the perfect solution to the problem, thank you again.

  5. xopi

    Hello,

    I still having Problems, because i use Ionic Sapi Rewriter, and i need to get the rewritten URIs but PHP_SELF has the original URI…

    so you have any solution?

    greetz!

  6. Justin

    Thank you, sir! This fixed me up with a concrete5 project that my client has hosted on IIS.

  7. Shuvo

    Thanks a lot, it really works like magic :)

  8. Thank you! i need to assign this value to a variable. when i use this code:

    $addition1 = if(!isset($_SERVER['REQUEST_URI']))
    {
           $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
           if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING']; }
    }
    

    i get an error: unexpected if statement…………

    any help is much appreciated.

  9. Ankit Tayal

    Thanks a lot .Great Work.saved a lot f time….I really very thankful to u for the same.

  10. Amit

    Hi,

    Thanks for that information.I am a .net developer and new to php development. I faced lot of problem for the same issue until I browse to this site.
    Problem solved.

    Thanks a million

  11. Rob

    Thank you very much – I was having issues with timthumb.php in WordPress, which needed to be installed on a server running IIS. This saved me a lot of trouble.

    • @Rob – where/what changes did you make exactly in timthumb.php?

  12. Thank You for taking the time to share this solution with us. It’s greatly appreciated. I’m having headaches with the IIS all the time and this time you saved me a lot of time.

    Cheers,
    Daniel

  13. Great post! I have been stopped by this problem for a few days.

  14. Thanks for that solution with IIS,
    but in which file do put this piece of code ?
    That should be a common file, called by any other files ?
    thanks for answer.

  15. ima

    Thanks , this helped me to get rid of yii error on iis6

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!