php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64951 SCRIPT_NAME acts strange
Submitted: 2013-05-31 07:28 UTC Modified: 2013-06-03 07:29 UTC
From: a dot panek at brainsware dot org Assigned: ab (profile)
Status: Not a bug Package: Built-in web server
PHP Version: 5.4.15 OS: Linux & Windows
Private report: No CVE-ID: None
 [2013-05-31 07:28 UTC] a dot panek at brainsware dot org
Description:
------------
When sending a request that has a file extension OR the request doesn't go to a 
index.php (a test.php, e.g.), the SCRIPT_NAME holds the URI instead of the actual 
current script name as documented here 
http://php.net/manual/en/reserved.variables.server.php

"Contains the current script's path. This is useful for pages which need to point 
to themselves. The __FILE__ constant contains the full path and filename of the 
current (i.e. included) file."

So basically the built-in server doesn't act as documented except when you use a 
file called "index.php" as router script and don't use a file extension in your 
URI.

I tested this on Linux and Windows resulting in the exact same behaviour.

Test script:
---------------
<?php
# index.php
#
# php -S localhost:8080 -t. index.php
#
# curl -i -X GET -H 'Accept: application/json' localhost:8080/something.json
# -> '/something.json'
# curl -i -X GET -H 'Accept: application/json' localhost:8080/something
# -> '/index.php
#
# php -S localhost:8080 -t. test.php
# -> always the uri

echo $_SERVER['SCRIPT_NAME'];

?>

Expected result:
----------------
/index.php

Actual result:
--------------
/something.json

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-03 07:29 UTC] ab@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: ab
 [2013-06-03 07:29 UTC] ab@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Using the router script in the command line is the same as using mod_rewrite 
with apache, basically something like this

RewriteEngine On
RewriteRule ^/hellou$ /hello.php [L]

is equivalent to

php -S localhost:8080 -t. hello.php

With this, requesting /hellou under apache will result 
$_SERVER['SCRIPT_NAME'] => /hellou
__FILE__ => full path to hello.php


If you ommit the router script, you'll get the apache behaviour without 
mod_rewrite. To do so, in your example 

php -S localhost:8080 -t.

The current document root will be like a normal htdocs with apache.
 [2013-06-03 12:28 UTC] a dot panek at brainsware dot org
1) I am using the router script according to the example in the documentation: 
http://php.net/manual/en/features.commandline.webserver.php#example-381 ("Example #4 Checking 
for CLI Web Server Use", "To reuse a framework router script during development with the CLI 
web server and later also with a production web server:")

This implies that I can reuse my current index.php, put in checks whether a file exists, and 
keep the rest as-is. I can't do this right now because SCRIPT_NAME doesn't behave as-is, but 
instead as if I had Apache + mod_rewrite enabled - as you said. I can't seem to find that 
behaviour documented anywhere?

That seems like either a bug in the documentation or the server.

2) If my router script is not called "index.php" but something else, SCRIPT_NAME *always* holds 
the URI instead of the actual script path. I haven't seen that documented either. (And it feels 
wrong to have that behaviour without any documentation about it)

3a) If I do not use a router script and try to access a URI with a file extension (like 
/something.json), the server always returns a 404 if given URI doesn't point to an actual file.

Even if this is not considered a bug, it's not good to be that unpredictable.
 [2013-06-03 12:38 UTC] a dot panek at brainsware dot org
Oh and what I forgot to mention: I currently can't use __FILE__ because the code 
using SCRIPT_NAME is not in the actual index.php (aka router script) but in 
another part of the framework.

The actual index.php also doesn't have any code besides the composer autoloader 
and the framework "boot" script being required.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 02 15:01:30 2024 UTC