11

In my local environment (PHP 5.4.24 on Apache 2.2.26) $_SERVER["SCRIPT_URL"] is present, but I read around that this variable is not always present, even though I couldn't find exactly when it is and when it isn't.

I'd like to know what is required for this variable to be reliably present.

clami219
  • 2,958
  • 1
  • 31
  • 45

2 Answers2

9

It's only available when you have mod_rewrite enabled:

http://httpd.apache.org/docs/2.0/rewrite/rewrite_intro.html#EnvVar

Wolfgang Stengel
  • 2,867
  • 1
  • 17
  • 22
  • No, they are there even without mod_rewrite, the are even there when you don't use Apache. Not everyone uses Apache! – Daniel W. Jun 26 '14 at 11:23
  • 2
    Of course you can simulate the field with other software, but mod_rewrite is the original source. I assume the question is, if the code uses the field, what would be a case in which the code would stop working. – Wolfgang Stengel Jun 26 '14 at 11:35
  • You both are right, I'm sorry. If you hit the edit button I'll change my short minded downvote to something positive – Daniel W. Jun 26 '14 at 11:44
  • There would seem to be _something else_ that can ultimately determine whether these environment variables are set. I have three Apache/mod_rewrite servers, yet `SCRIPT_URL` and `SCRIPT_URI` are only set on one of them? – MrWhite Oct 12 '16 at 23:08
  • Looking a new bug on some old code it seems that even with mod_rewrite enabled I _used to_ have $_SERVER['SCRIPT_URL'] on a server last year and now I do not! Had to switch to REQUEST_URI – thinsoldier Jan 22 '18 at 21:49
6

This variable strongly depends on the server's configuration.

When using nginx with php5-fpm (fcgi) for example, you'd pass the variable as fpm_parameter:

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

There is a similiar configuration for scgi.

There are 3 types of global variables in $_SERVER, some are taken from the client's request header and are not reliable, some are set by PHP/Webserver (for example REMOTE_ADDR) and are very reliable, and some depend on your configuration which can be reliable depending on that configuration.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151