Loading

PHP Global Variable $_SERVER

PHP Global Variable $_SERVER is used for server and execution environment information. It is an array. Basically, it contains information on the header, paths, and script location.

The following indices we can use with $_SERVER:-

$_SERVER[‘PHP_SELF’]:- It returns the filename of the currently executing script and the relative of the document root.

Example:-

http://example.com/user/signup.php

It will return /user/signup.php

$_SERVER[‘GATEWAY_INTERFACE’]:- It will return the version of the Common Gateway Interface (CGI) the server is using.

Example:- ‘CGI/1.1’

$_SERVER[‘SERVER_ADDR’]:- It will return the IP address of the host server, Where the current script is executing.

Example:- 127.0.0.1

$_SERVER[‘SERVER_NAME’]:- It will return the name of the host server Where the current script is executing.

Example:- www.kshtutor.com

$_SERVER[‘SERVER_SOFTWARE’]:- It will return the server identification string.

Example :- Apache/2.4.51 (Win32) PHP/7.4.26

$_SERVER[‘SERVER_PROTOCOL’]:- It will return the name and revision of the information protocol from the requested page.

Example:- HTTP/1.1

$_SERVER[‘REQUEST_METHOD’]:- It will return the request method used to access the page. Like  POST or GET. The default will be GET.

$_SERVER[‘REQUEST_TIME_FLOAT’]:- It will return the timestamp of the start of the request with microsecond precision.

Example:- 1688688461.6974

$_SERVER[‘REQUEST_TIME’]:- It will return the timestamp of the start of the request.

Example:- 1688688214

$_SERVER[‘QUERY_STRING’]:-It will return the query string if the page is accessed via a query string.

Example:-

https://www.kshtutor.com/First.php?a=9

output:- a=9

$_SERVER[‘DOCUMENT_ROOT’]:- It will return the document root directory under which the current script is executing, as defined in the server’s configuration file.

Example:- C:/wamp/www

$_SERVER[‘HTTP_ACCEPT’]:- it will return the Accept header from the current request.

Example :- text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8

$_SERVER[‘HTTP_ACCEPT_CHARSET’] :- It will return the Accept_Charset header from the current request.

$_SERVER[‘HTTP_HOST’]:- It will return the Host header from the current request.

Example:- localhost

$_SERVER[‘HTTP_REFERER’]:- It will return the complete URL of the current page. All user agents do not support it.

$_SERVER[‘HTTPS’] :- It set the non-empty value. And it Is the script queried through a secure HTTP protocol.

$_SERVER[‘REMOTE_ADDR’] :- It will return the current page IP address.

Example:- 127.0.0.1

$_SERVER[‘REMOTE_HOST’]:- It will return the Host name of the current page.

$_SERVER[‘REMOTE_PORT’]:- It will return the port being used on the user’s machine to communicate with the web server.

Example:- 59084

$_SERVER[‘SCRIPT_FILENAME’]:- It will return the absolute pathname of the currently executing script.

Example:- C:/wamp/www/kshtutor/First.php

$_SERVER[‘SERVER_ADMIN’]:- It will return the value given to the SERVER_ADMIN for the 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:- wampserver@wampserver.invalid(virtual host)

$_SERVER[‘SERVER_PORT’]:-  It will return the port on the server machine being used by the web server for communication.

Example:-  80       \

Note: In  Apache 2 configuration, UseCanonicalName = On, as well as UseCanonicalPhysicalPort = On must be set in order to get the physical (real) port, and it may or may not return the physical port value. It is not safe to rely on this value in security-dependent contexts.

$_SERVER[‘SERVER_SIGNATURE’]:- It will return the server version and virtual host name which are added to server-generated pages.

Example :- Apache/2.4.51 (Win32) PHP/7.4.26 Server at localhost Port 80

$_SERVER[‘PATH_TRANSLATED’]:- It will return the file system-based path to the current script. It never returns the document root.

$_SERVER[‘SCRIPT_NAME’]:- It will return the path of the current script.

Example:- /kshtutor/First.php

$_SERVER[‘SCRIPT_URI’]:-It will return the URI of the current page.

$_SERVER[‘REQUEST_URI’]:- It will return the URI which was given in order to access the current page.

Example:- /kshtutor/First.php

$_SERVER[‘PHP_AUTH_DIGEST’]:- When doing Digest HTTP authentication this variable is set to the ‘Authorization’ header sent by the client.

$_SERVER[‘PHP_AUTH_USER’]:-  When doing HTTP authentication this variable is set to the username provided by the user.

$_SERVER[‘PHP_AUTH_PW’]:- When doing HTTP authentication this variable is set to the password provided by the user.

$_SERVER[‘AUTH_TYPE’]:-When doing HTTP authentication this variable is set to the authentication type.

$_SERVER[‘PATH_INFO’]:- It will return containing any client-provided pathname information trailing the actual script filename but preceding the query string.

$_SERVER[‘ORIG_PATH_INFO’] :- The original version of ‘PATH_INFO’ before processed by PHP.

Read Also:-

PHP VARIABLES

PHP Global Variable $_GET

PHP $GLOBALS Variable

PHP predefined variables

How to code a PHP application

For more detail about variables please see manual

Share with:


Leave a Reply

Connect with:

z35W7z4v9z8w
Verified by ExactMetrics