I have a php framework and I used $_SERVER['SCRIPT_NAME']
to optimize portability. That way I don't need to manually configure the path anymore.
$this->base_url = str_replace('index.php', '', 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME']);
But I noticed that $_SERVER['SCRIPT_NAME']
and $_SERVER['PHP_SELF']
returns the exact same string. So, what's the difference? How should I choose between them?
Description. $_SERVER['PHP_SELF'] Returns the filename of the currently executing script. $_SERVER['GATEWAY_INTERFACE'] Returns the version of the Common Gateway Interface (CGI) the server is using.
$_SERVER['REQUEST_URI'] contains the URI of the current page. So if the full path of a page is https://www.w3resource.com/html/html-tutorials.php, $_SERVER['REQUEST_URI'] would contain /html/html-tutorials. php. Following php code used $_SERVER['REQUEST_URI'] variable.
$_SERVER['PHP_SELF'] variable. This array element points out the filename of the currently executing script. For example, if you run www.cyberciti.biz/index.php, $_SERVER['PHP_SELF'] would be /index.
$_SERVER is a superglobal that holds information regarding HTTP headers, path and script location etc. All the server and execution environment related information is available in this associative array. Most of the entries in this array are populated by web server.
Difference
http://sandbox.phpcode.eu/g/3e38d.php/test
Script name is absolute path to file.
PHP_SELF
is script you're currently in (along with "path" after .php
)
It's like $_SERVER['SERVER_NAME']
and $_SERVER['HTTP_HOST']
http://sandbox.phpcode.eu./g/f5093.php
http://sandbox.phpcode.eu/g/f5093.php
spot one difference
SCRIPT_NAME
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.
PHP_SELF
The filename of the currently executing script, relative to the document root. For instance,
$_SERVER['PHP_SELF']
in a script at the addresshttp://example.com/test.php/foo.bar
would be/test.php/foo.bar
. The__FILE__
constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.
source php.net
There is one thing:
Check out http://www.yoursite.com/example/index.php/dir/test
in $_SERVER['PHP_SELF'] == '/example/index.php/dir/test';
in $_SERVER['SCRIPT_NAME'] == '/example/index.php';
ETA: Tried myself
Tried this on localhost http://127.0.0.1:8887/index.php/dir/test
echo $_SERVER['PHP_SELF'] . "<br />";
echo $_SERVER['SCRIPT_NAME'];
Output is:
/index.php/dir/test
/index.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With