Sie sind auf Seite 1von 21

$_SERVER

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.

$_SERVER has following basic properties:

1. Set by web server.

2. Directly related to the runtime environment of the current php script.

3. It does the same job as $HTTP_SERVER_VARS used to do in previous versions of PHP

Sample output of $_SERVER taken from localhost

PHP : $_SERVER['PHP_SELF']

PHP : $_SERVER['argv']

PHP : Super global variable: $argc

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']

PHP_SELF is the name of the currently executing script.

http://www.yoursite.com/example/ -- --> /example/index.php

http://www.yoursite.com/example/index.php -- --> /example/index.php

http://www.yoursite.com/example/index.php?a=test -- --> /example/index.php

http://www.yoursite.com/example/index.php/dir/test -- --> /dir/test

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']

REQUEST_URI refers to the URI given to access a page.


http://www.yoursite.com/example/ -- --> /

http://www.yoursite.com/example/index.php -- --> /example/index.php

http://www.yoursite.com/example/index.php?a=test -- --> /example/index.php?a=test

http://www.yoursite.com/example/index.php/dir/test -- --> /example/index.php/dir/test

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.

http://www.yoursite.com/example/ -- --> /example/index.php

http://www.yoursite.com/example/index.php -- --> /example/index.php

http://www.yoursite.com/example/index.php?a=test -- --> /example/index.php

http://www.yoursite.com/example/index.php/dir/test -- --> /example/index.php

PHP $_SERVER

$_SERVER is a PHP super global variable which holds information about headers, paths, and script
locations.

This example shows how to use some of the elements in $server

<?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.

Following php code used $_SERVER['PHP_SELF']

<?php

echo $_SERVER['PHP_SELF'];

?>

Copy
View the example in browser

Another advanced example:


Basic knowledge of array is a prerequisite of this example. You can get back to
this example after you learn php array. The following PHP code compares the
array elements with the $_SERVER['PHP_SELF'] and displays a message.

<?php

$findit=array('/php/super-variables/test.php',

'/php/super-variables/test1123.php',

'/php/super-variables/php-self-advanced-example1.php'

);

for ($j=0; $j<count($findit); $j++)

if ($_SERVER['PHP_SELF']==$findit[$j])

echo "You are learning PHP Super Globals";

?>

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:\

Output of the above command is :

d:\arguments.php
w3resource
.
com

PHP: Super global variable: $argc

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

C:\wamp\bin\php\php5.3.0>php.exe-f "C:\argc.php" -- -arg1 -arg2


-arg3 int(4)
C:\wamp\bin\php\php5.3.0>
PHP: $_SERVER['GATEWAY_INTERFACE']

$_SERVER['GATEWAY_INTERFACE'] variable returns the version of the


Common Gateway Interface (CGI) specification the server is using. For example
CGI/1.1 is a valid GATEWAY_INTERFACE.

Following php code used $_SERVER['GATEWAY_INTERFACE']. Notice one


point, if any server does not contain Common Gateway Interface (CGI), it won't
display anything.

Example:

<?php

echo $_SERVER['GATEWAY_INTERFACE'];

?>

Copy
PHP: $_SERVER['SERVER_ADDR']

The $_SERVER['SERVER_ADDR'] returns the IP address (Internet Protocol


address) of the host server.

Following php code used $_SERVER['SERVER_ADDR'] to display the IP


address of the host server.

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']

States name of the host server.

Following php code used $_SERVER['SERVER_NAME'] variable to display


name of the host server.
Example:

<?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']

$_SERVER['SERVER_SOFTWARE'] variable fetches the server identification


string. The string is provided in the headers when responding to requests.

Following php code used $_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.

Following php code used $_SERVER['SERVER_PROTOCOL']

Example:

<?php

echo $_SERVER['SERVER_PROTOCOL'];

?>

Copy
Output :

HTTP/1.1

View the example in browser

PHP: $_SERVER['REQUEST_METHOD']

$_SERVER['REQUEST_METHOD'] fetches the request method used to access


the page. Request methods are 'GET', 'HEAD', 'POST', 'PUT'.

Following php code used $_SERVER['REQUEST_METHOD'] -

Example:

<?php

echo $_SERVER['REQUEST_METHOD'];

?>

Copy
Output:

GET
View the example in browser
PHP: $_SERVER['REQUEST_TIME']

$_SERVER['REQUEST_TIME'] variable fetches the timestamp of the start of the


request.

Following php code used $_SERVER['REQUEST_TIME']

Example:

<?php

echo $_SERVER['REQUEST_TIME'];

?>

Copy
View the example in browser

PHP: $_SERVER['QUERY_STRING']

If a page is accessed via any query string, $_SERVER['QUERY_STRING']


fetches that query string.

Following php code used $_SERVER['QUERY_STRING'].

Example:

<?php

echo "The query string is: ".$_SERVER['QUERY_STRING'];

?>

Copy
If the above php code is saved with a filename of QUERY_STRING.php and if
you add '?tutorial=php&section=super-globals' (i.e.
QUERY_STRING.php?tutorial=php&section=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'].

Following php code used $_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']

If exists, contents of the Accept-Charset: header from the current request is


fetched by $_SERVER['HTTP_ACCEPT_CHARSET'].

Following php code used $_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.

Following php code used $_SERVER['HTTP_HOST']

Example :

<?php

echo $_SERVER['HTTP_HOST'];

?>

Copy
Output:

www.w3resource.com
View the example in browser

PHP: $_SERVER['HTTP_REFERER']

$_SERVER['HTTP_REFERER'] can be used to fetch the complete URL of the


current page.

Since some but not all user agents (for example browsers) don't support it, usage
of this is not much reliable.

Following php code used $_SERVER['HTTP_REFERER']

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'].

Following php code used $_SERVER['HTTP_USER_AGENT']

Example:

<?php

echo $_SERVER['HTTP_USER_AGENT'];

?>

Copy
View the example in browser

PHP: $_SERVER['HTTPS']

If the script was queried through a secure HTTP protocol, $_SERVER['HTTPS']


is set to a non-empty value.

Following php code used $_SERVER['HTTPS']

Example:

<?php

echo $_SERVER['HTTPS'];

?>
Copy
PHP: $_SERVER['REMOTE_ADDR']

$_SERVER['REMOTE_ADDR'] fetches the IP address from which the user is


viewing the current page.

Following php code used $_SERVER['REMOTE_ADDR']

Example:

<?php

echo $_SERVER['REMOTE_ADDR'];

?>

Copy
View the example in browser

PHP : $_SERVER['REMOTE_HOST']

$_SERVER['REMOTE_HOST'] fetches the Host name from which the user is


viewing the current page.

But for this script to work, Hostname Lookups On inside httpd.conf must be
configured.

Following php code used $_SERVER['REMOTE_HOST']

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.

Following php code used $_SERVER['REMOTE_PORT']

Example:

<?php

echo $_SERVER['REMOTE_PORT'];

?>

Copy
View the example in browser

PHP: $_SERVER['SCRIPT_FILENAME']

$_SERVER['SCRIPT_FILENAME'] fetches the absolute pathname of the


currently executing script.

Following php code used $_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.

Following php code used $_SERVER['SERVER_ADMIN']

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.

Following php code used $_SERVER['SERVER_PORT']

Example:

<?php

echo $_SERVER['SERVER_PORT'];

?>

Copy
View the example in browser
PHP : $_SERVER['SERVER_SIGNATURE']

$_SERVER['SERVER_SIGNATURE'] fetches the string containing the server


version and virtual host name which are added to server-generated pages, if
enabled.

Following php code used $_SERVER['SERVER_SIGNATURE']

Example:

<?php

echo $_SERVER['SERVER_SIGNATURE'];

?>

Copy
Output:

Apache/2.2.3 (CentOS) Server at www.w3resource.com Port 80


View the example in browser

PHP: $_SERVER['PATH_TRANSLATED']

$_SERVER['PATH_TRANSLATED'] fetches the file system based path to the


current script. This is accomplished after the server has done any virtual-to-real
mapping.

It works only when PATH_INFO is defined.

Following php code used $_SERVER['PATH_TRANSLATED']

Example:

<?php

echo $_SERVER['PATH_TRANSLATED'];
?>

Copy
PHP: $_SERVER['SCRIPT_NAME']

$_SERVER['SCRIPT_NAME'] contains the path of the current script.

Example:

<?php

echo $_SERVER['SCRIPT_NAME'];

?>

Copy
Output:

/php/super-variables/$_SERVER-SERVER_SCRIPT_NAME-example1.php
PHP: $_SERVER['SCRIPT_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.

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

Das könnte Ihnen auch gefallen