Knowing $_SERVER PHP Variable in a better way

$_SERVER array in PHP is very useful in getting lot of information about the PHP script in execution as well as the server/request details. $_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. Following are few very useful attributes of $_SERVER array:

$_SERVER[‘PHP_SELF’]

This is 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. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. However, unlike $_SERVER[‘SCRIPT_NAME’], it provides additional path information like $_SERVER[‘REQUEST_URI’] when the actual php file is present in the path. So when the $_SERVER[‘REQUEST_URI’] is /index.php/big/directory/ then $_SERVER[‘PHP_SELF’] will be /index.php/big/directory/. However if all the URI’s under http://www.example.com/ is mapped to http://www.example.com/index.php, then, for example, http://www.example.com/abc/def will return /index.php like $_SERVER[‘SCRIPT_NAME’]. Note that $_SERVER[‘REQUEST_URI’] data is ignored for this request. $_SERVER[‘PHP_SELF’] is supported on all platforms.

$_SERVER[‘SERVER_ADDR’]

Gives you the IP address of server on which the current script is executing. Thus for printing IP address of server use following code snippet.
<?php echo "Server IP Address is $_SERVER['SERVER_ADDR']" ?>
Code language: PHP (php)

$_SERVER[‘REMOTE_ADDR’]

$_SERVER[‘REMOTE_ADDR’] returns the IP address from which the user is viewing the current page. REMOTE_ADDR will not written correct IP address if the client is behind a proxy. In that case use the script mention in this tutorial: PHP Code Snippet

$_SERVER[‘REQUEST_URI’]

The actual URI used in HTTP protocol (after GET or POST) to make the request. For example if you access this site with the following command: GET / HTTP/1.0 Host: viralpatel.net/ Then $_SERVER[‘REQUEST_URI’] will be /. However is you access the same page using: GET /index.php HTTP/1.0 Host: viralpatel.net/ Then $_SERVER[‘REQUEST_URI’] will be /index.php. Although $_SERVER[‘SCRIPT_NAME’] and $_SERVER[‘PHP_SELF’] will be /index.php in both cases. $_SERVER[‘REQUEST_URI’] is not supported on IIS (with or without SSL).

$_SERVER[”HTTP_USER_AGENT’]

Contents of the User-Agent: header from the current request, if there is one. This is a string denoting the user agent being which is accessing the page. A typical example is: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586).

View Comments

Share
Published by
Viral Patel
Tags: $_SERVER PHP php variable Tutorial

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

4 years ago