PHP Global Variables

Before going deep into the PHP, a PHP developer or programmer should know about some important PHP Global Variables and their use at the occasion. So in this tutorial I am showing you the important variables and their brief introduction.

Some global variables can be created through the use of define() and others are reserved because of a special function. These variables provide access to different types of data. All global variables listed below are arrays:-

variables

1.   $_SERVER

$_SERVER[‘HTTP_USER_AGENT’]

Browser description from header

[HTTP_USER_AGENT] => Mozilla/5.0 (X11; U; Linux i686; en-US; rv:

1.8.1.12); Firefox/2.0.0.12

 

$_SERVER[‘HTTP_REFERER’]

The page address that referred the user

[HTTP_REFERER] => http://www.example.com/index.htm

 

$_SERVER[‘REMOTE_ADDR’]

The client’s IP address

[REMOTE_ADDR] => 127.0.0.1

 

$_SERVER[‘DOCUMENT_ROOT’]

System root location of current script

[DOCUMENT_ROOT] => /opt/lampp/htdocs

 

$_SERVER[‘SCRIPT_FILENAME’]

Absolute path of current script

[SCRIPT_FILENAME] => /opt/lampp/htdocs/test.php

 

$_SERVER[‘REQUEST_URI’]

The Universal Resource Identifier for the page

[REQUEST_URI] => /test.php?test=value

 

$_SERVER[‘SCRIPT_NAME’]

The current scripts path

[SCRIPT_NAME] => /test.php

 

$_SERVER[‘QUERY_STRING’]

The current scripts path

[QUERY_STRING] => test=value

 

$_SERVER[‘PHP_SELF’]

The name of the current script, relative to the root

[PHP_SELF] => /test.php

When submitting a form to the same page/file that contains it, you can use

the $_SERVER[‘PHP_SELF’] variable to dynamically provide the location.

<form method=”POST” action=”<?php  echo $SERVER[‘PHP_SELF’]; ?>”>

<form method=”POST” action=”filename.php”>

 2.   $_REQUEST

Includes all variables provided by $_GET, $_POST, and $_COOKIE

3.  $_POST

Includes all variables submitted through HTTP POST, such as an HTML

form with action=”post”.

 4.   $_GET

Includes all variables submitted through the query string url, either manually or

from a form with action=”get”.

http://www.example.com/test.php?query=value

 5.  $_SESSION

Variables assigned to the current session.

 6.  $_COOKIE

Any cookies stored for the current website. Only visible after the page was

reloaded if it was just set using setcookie().

setcookie() – Assigning and deleting cookies

 7.  $_FILES

Variables provided to the script via POST uploads.

 8.  $_ENV

A collection of variables about the server environment.

 9.  $GLOBALS

Contains a reference for all variables, global or otherwise, in the script.

Leave a Reply

Your email address will not be published. Required fields are marked *