Beruflich Dokumente
Kultur Dokumente
is an array which holds information of headers, paths, script locations. Web server creates the entries in
the array. This is not assured that every web server will provide similar information, rather some servers
may include or exclude some information which are not listed here.
PHP : $_SERVER['PHP_SELF']
PHP : $_SERVER['argv']
PHP : $_SERVER['GATEWAY_INTERFACE']
PHP : $_SERVER['SERVER_ADDR']
PHP : $_SERVER['SERVER_NAME']
PHP : $_SERVER['SERVER_SOFTWARE']
PHP : $_SERVER['SERVER_PROTOCOL']
PHP : $_SERVER['REQUEST_METHOD']
PHP : $_SERVER['REQUEST_TIME']
PHP : $_SERVER['QUERY_STRING']
PHP : $_SERVER['HTTP_ACCEPT']
PHP : $_SERVER['HTTP_ACCEPT_CHARSET']
PHP : $_SERVER['HTTP_HOST']
PHP : $_SERVER['REMOTE_PORT']
PHP : $_SERVER['SCRIPT_FILENAME']
PHP : $_SERVER['SERVER_ADMIN']
PHP : $_SERVER['SERVER_PORT']
PHP : $_SERVER['SERVER_SIGNATURE']
PHP : $_SERVER['PATH_TRANSLATED']
PHP : $_SERVER['SCRIPT_NAME']
PHP : $_SERVER['SCRIPT_URI']
$_SERVER is one of the PHP global variables—termed Superglobals—which contain information about
server and execution environments. These are pre-defined variables so they are always accessible from
any class, function or file.
The entries here are recognized by web servers, but there is no guarantee that each web server
recognizes every Superglobal. These three PHP $_SERVER arrays all behave in similar ways—they return
information about the file in use. When exposed to different scenarios, in some cases they behave
differently. These examples may help you decide which is best for what you need. A full list of $_SERVER
arrays is available at the PHP website.
$_SERVER['PHP_SELF']
When you use $_SERVER[’PHP_SELF’], it returns the file name /example/index.php both with and
without the file name typed in the URL. When variables are appended at the end, they were truncated
and again /example/index.php was returned. The only version that produced a different result has
directories appended after the file name. In that case, it returned those directories.
$_SERVER['REQUEST_URI']
All of these examples returned exactly what was entered for the URL. It returned a plain /, the file name,
the variables, and the appended directories, all just as they were entered.
$_SERVER['SCRIPT_NAME']
SCRIPT_NAME is the current script's path. This comes in handy for pages that need to point to
themselves.
PHP $_SERVER
$_SERVER is a PHP super global variable which holds information about headers, paths, and script
locations.
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
Note: Prior to PHP 5.4.0, $HTTP_SERVER_VARS contained the same initial information, but was not a
superglobal. (Note that $HTTP_SERVER_VARS and $_SERVER were different variables and that PHP
handled them as such.)
PHP: $_SERVER['PHP_SELF']
You can find the filename of the currently executing script by using
$_SERVER['PHP_SELF']. Filename shown as output is relative to the root of the
document.
<?php
echo $_SERVER['PHP_SELF'];
?>
Copy
View the example in browser
<?php
$findit=array('/php/super-variables/test.php',
'/php/super-variables/test1123.php',
'/php/super-variables/php-self-advanced-example1.php'
);
if ($_SERVER['PHP_SELF']==$findit[$j])
?>
Copy
View the example in browser
PHP: $_SERVER['argv']
If used, all the arguments passed from command line, are stored in
$_SERVER['argv'] array. The file name itself is the first item in the array, i.e. 0.
Example:
<?php
echo $_SERVER['argv'];
?>
Copy
If this is a file (say arguments.php) containing $_SERVER['argv'], and then you
run the following from command line :
D:\php\php.exe d:\arguments.php w3resource . com
where php is installed in D:\php and arguments.php is saved in D:\
d:\arguments.php
w3resource
.
com
$argc can contain a number of command line parameters. These parameters are
passed to the corresponding script.
Example:
<?php
var_dump($argc);
?>
Copy
If the code above is saved as argc.php and executed from command prompt with
arguments arg1, arg2 and arg3; here is the output -
Example:
<?php
echo $_SERVER['GATEWAY_INTERFACE'];
?>
Copy
PHP: $_SERVER['SERVER_ADDR']
Example:
<?php
echo $_SERVER['SERVER_ADDR'];
?>
Copy
Sample output :
xxx.xx.xxx.xx
Note : If you run the above code on localhost it will return 127.0.0.1
PHP: $_SERVER['SERVER_NAME']
<?php
echo $_SERVER['SERVER_NAME'];
?>
Copy
Output:
www.w3resource.com
View the example in browser
Note : If you run the above code on localhost, by default it will show the server
name as localhost. If the script is running on a virtual host, the name of the virtual
host set, will be returned as server name.
PHP: $_SERVER['SERVER_SOFTWARE']
Example:
<?php
echo $_SERVER['SERVER_SOFTWARE'];
?>
Copy
Output:
Apache/2.2.3 (CentOS)
View the example in browser
PHP: $_SERVER['SERVER_PROTOCOL']
$_SERVER['SERVER_PROTOCOL'] variable fetches the name and revision of
the information protocol via which the page has been requested.
Example:
<?php
echo $_SERVER['SERVER_PROTOCOL'];
?>
Copy
Output :
HTTP/1.1
PHP: $_SERVER['REQUEST_METHOD']
Example:
<?php
echo $_SERVER['REQUEST_METHOD'];
?>
Copy
Output:
GET
View the example in browser
PHP: $_SERVER['REQUEST_TIME']
Example:
<?php
echo $_SERVER['REQUEST_TIME'];
?>
Copy
View the example in browser
PHP: $_SERVER['QUERY_STRING']
Example:
<?php
?>
Copy
If the above php code is saved with a filename of QUERY_STRING.php and if
you add '?tutorial=php§ion=super-globals' (i.e.
QUERY_STRING.php?tutorial=php§ion=super-globals); it will print this string
in the page since you have asked the script to print
$_SERVER['QUERY_STRING'].
PHP: $_SERVER['HTTP_ACCEPT']
If exists, contents of the Accept: header from the current request is fetched by
$_SERVER['HTTP_ACCEPT'].
Example:
<?php
echo $_SERVER['HTTP_ACCEPT'];
?>
Copy
Output:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
View the example in browser
PHP: $_SERVER['HTTP_ACCEPT_CHARSET']
Example:
<?php
echo $_SERVER['HTTP_ACCEPT_CHARSET'];
?>
Copy
Output:
ISO-8859-1,utf-8;q=0.7,*;q=0.7
View the example in browser
PHP: $_SERVER['HTTP_HOST']
States name of the host server Contents of the Host: header from the current
request, if there is one.
Example :
<?php
echo $_SERVER['HTTP_HOST'];
?>
Copy
Output:
www.w3resource.com
View the example in browser
PHP: $_SERVER['HTTP_REFERER']
Since some but not all user agents (for example browsers) don't support it, usage
of this is not much reliable.
Example:
<?php
echo $_SERVER['HTTP_REFERER'];
?>
Copy
Output:
https://www.w3resource.com/php/super-variables/$_SERVER-
HTTP_REFERER.php
View the example in browser
PHP: $_SERVER['HTTP_USER_AGENT']
If exists, contents of the User-Agent: header from the current request is fetched
by $_SERVER['HTTP_USER_AGENT'].
Example:
<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>
Copy
View the example in browser
PHP: $_SERVER['HTTPS']
Example:
<?php
echo $_SERVER['HTTPS'];
?>
Copy
PHP: $_SERVER['REMOTE_ADDR']
Example:
<?php
echo $_SERVER['REMOTE_ADDR'];
?>
Copy
View the example in browser
PHP : $_SERVER['REMOTE_HOST']
But for this script to work, Hostname Lookups On inside httpd.conf must be
configured.
Example:
<?php
echo $_SERVER['REMOTE_HOST'];
?>
Copy
PHP: $_SERVER['REMOTE_PORT']
States name of the host server. The port being used on the user's machine to
communicate with the web server.
Example:
<?php
echo $_SERVER['REMOTE_PORT'];
?>
Copy
View the example in browser
PHP: $_SERVER['SCRIPT_FILENAME']
Example:
<?php
echo $_SERVER['SCRIPT_FILENAME'];
?>
Copy
Output:
https://www.w3resource.com/php/super-variables/$_SERVER-
HTTP_REFERER.php
View the example in browser
PHP: $_SERVER['SERVER_ADMIN']
$_SERVER-SERVER_ADMIN.php fetches the value given to the
SERVER_ADMIN (for Apache) directive in the web server configuration file.
If the script is running on a virtual host, this will be the value defined for that
virtual host.
Example:
<?php
echo $_SERVER['SERVER_ADMIN'];
?>
Copy
Output:
abcd@w3resource.com
View the example in browser
PHP $_SERVER['SERVER_PORT']
States name of the host server The port on the server machine being used by the
web server for communication. For default setups, this will be '80'; using SSL, for
instance, will change this to whatever your defined secure HTTP port is.
Example:
<?php
echo $_SERVER['SERVER_PORT'];
?>
Copy
View the example in browser
PHP : $_SERVER['SERVER_SIGNATURE']
Example:
<?php
echo $_SERVER['SERVER_SIGNATURE'];
?>
Copy
Output:
PHP: $_SERVER['PATH_TRANSLATED']
Example:
<?php
echo $_SERVER['PATH_TRANSLATED'];
?>
Copy
PHP: $_SERVER['SCRIPT_NAME']
Example:
<?php
echo $_SERVER['SCRIPT_NAME'];
?>
Copy
Output:
/php/super-variables/$_SERVER-SERVER_SCRIPT_NAME-example1.php
PHP: $_SERVER['SCRIPT_URI']
Example:
<?php
echo $_SERVER['REQUEST_URI'];
?>
Copy
Output:
/php/super-variables/PHP-$_SERVER-REQUEST_URI-example.php
View the example in browser
<!DOCTYPE html>
<html>
<body>
</body>
</html>
another example:
<? php
echo $_SERVER['SERVER_NAME'];
?>
OUTPUT:
www.example.com