Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME']?

Tags:

php

apache

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?

like image 629
Hugo Mota Avatar asked Jul 16 '11 17:07

Hugo Mota


People also ask

What is $_ SERVER [' Php_self ']?

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.

What is $_ SERVER [' Request_uri ']?

$_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.

What is the $_ SERVER Php_self variable explain with example?

$_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.

What is the purpose of $_ SERVER variable in PHP?

$_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.


2 Answers

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

like image 65
genesis Avatar answered Nov 02 '22 08:11

genesis


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 address http://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
like image 34
Igoris Azanovas Avatar answered Nov 02 '22 07:11

Igoris Azanovas