PHP Global Variables

PHP Global Variables tutorial will explains you How to use PHP Global Variables? with examples & Syntax.

Several predefined variables in PHP are “superglobals”, which means they are available in all scopes, which implies that they are dependably approachable, paying little mind to extension – and you can access them from any function, class or file without needing to do anything special.

List of Global Variables in PHP
$_GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

PHP $GLOBAL

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods).

$GLOBALS — References all variables available in global scope

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array

Example on PHP $GLOBAL

<?php

$a = 15;
$b = 35;
 function addition(){
    $GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b'];
}
addition();
echo $c;

?>

PHP $_SERVER

PHP $_SERVER is a super global variable which holds data about headers, paths, and script areas. Variables set by the web server or otherwise directly related to the execution environment of the current script. Analogous to the old $HTTP_SERVER_VARS array (which is still available, but deprecated).

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI/1.1 specification, so you should be able to expect those.

Example on PHP $_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'];

?>

The following table lists the Description of elements that can go inside $_SERVER:

Element/CodeDescription
$_SERVER[‘GATEWAY_INTERFACE’]Returns the version of the Common Gateway Interface (CGI) the server is using
$_SERVER[‘SERVER_ADDR’]Returns the IP address of the host server
$_SERVER[‘SERVER_NAME’]Returns the name of the host server (such as www.w3schools.com)
$_SERVER[‘SERVER_SOFTWARE’]Returns the server identification string (such as Apache/2.2.24)
$_SERVER[‘SERVER_PROTOCOL’]Returns the name and revision of the information protocol (such as HTTP/1.1)
$_SERVER[‘REQUEST_METHOD’]Returns the request method used to access the page (such as POST)
$_SERVER[‘REQUEST_TIME’]Returns the timestamp of the start of the request (such as 1377687496)
$_SERVER[‘QUERY_STRING’]Returns the query string if the page is accessed via a query string
$_SERVER[‘HTTP_ACCEPT’]Returns the Accept header from the current request
$_SERVER[‘HTTP_ACCEPT_CHARSET’]Returns the Accept_Charset header from the current request (such as utf-8,ISO-8859-1)
$_SERVER[‘HTTP_HOST’]Returns the Host header from the current request
$_SERVER[‘HTTP_REFERER’]Returns the complete URL of the current page (not reliable because not all user-agents support it)
$_SERVER[‘HTTPS’]Is the script queried through a secure HTTP protocol
$_SERVER[‘REMOTE_ADDR’]Returns the IP address from where the user is viewing the current page
$_SERVER[‘REMOTE_HOST’]Returns the Host name from where the user is viewing the current page
$_SERVER[‘REMOTE_PORT’]Returns the port being used on the user’s machine to communicate with the web server
$_SERVER[‘SCRIPT_FILENAME’]Returns the absolute pathname of the currently executing script
$_SERVER[‘SERVER_ADMIN’]Returns the value given to the SERVER_ADMIN directive in the web server configuration file (if your script runs on a virtual host, it will be the value defined for that virtual host) (such as [email protected])
$_SERVER[‘SERVER_PORT’]Returns the port on the server machine being used by the web server for communication (such as 80)
$_SERVER[‘SERVER_SIGNATURE’]Returns the server version and virtual host name which are added to server-generated pages
$_SERVER[‘PATH_TRANSLATED’]Returns the file system based path to the current script
$_SERVER[‘SCRIPT_NAME’]Returns the path of the current script
$_SERVER[‘SCRIPT_URI’]Returns the URI of the current page

PHP $_REQUEST:

PHP $_REQUEST is used to collect data after submitting an HTML form. Variables provided to the script via any user input mechanism, and which therefore cannot be trusted. The presence and order of variable inclusion in this array is defined according to the variables_order configuration directive. This array has no direct analogue in versions of PHP prior to 4.1.0

The example below shows a form with an input field and a submit button. When a user submits the data by clicking on “Submit”, the form data is sent to the file specified in the action attribute of the <form> tag. In this example, we point to this file itself for processing form data. If you wish to use another PHP file to process form data, replace that with the filename of your choice. Then, we can use the super global variable $_REQUEST to collect the value of the input field:

Example:

<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="uname">
<input type="submit">
</form>
<?php
$name = $_REQUEST['uname'];
echo $name;
?>
</body>
</html>

Note: When running on the command line , this will not include the argv and argc entries; these are present in the $_SERVER array.

PHP $_POST

Most forms use the POST method because it “hides” form data from user since it doesn’t get attached with the URL in the address bar. This data is collected using $_POST variable (which is in array format) in php script

The example below shows a form with an input field and a submit button. When a user submits the data by clicking on “Submit”, the form data is sent to the file specified in the action attribute of the <form> tag. In this example, we point to the file itself for processing form data. If you wish to use another PHP file to process form data, replace that with the filename of your choice. Then, we can use the super global variable $_POST to collect the value of the input field:

Example:

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname" value="Hello World">
<input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>

</body>
</html>

We got Out Put After Press Submit Button.

Out Put

Hello World

$_GET

PHP GET method sends its variables through web browsers URL, which makes it visible and possibly change the information that was sent. Variables provided to the script via HTTP GET. Analogous to the old $HTTP_GET_VARS array (which is still available, but deprecated).

Example

myform.html:

<form action="collect.php" method="GET">
Name: <input type="text" name="name" />
Email: <input type="text" name="email" />
<input type="submit" />
</form>

collect.php

<? php
echo $_GET["name"] .<br />";
echo $_GET["email"];
?>

PHP $_FILES

An associative array of items uploaded to the current script via the HTTP POST method. The structure of this array is outlined in the POST method uploads section.
$HTTP_POST_FILES contains the same initial information, but is not a superglobal. (Note that $HTTP_POST_FILES and $_FILES are different variables and that PHP handles them as such)

$_FILES is a super global variable which can be used to upload files. Here we will see an example in which our php script checks if the form to upload the file is being submitted and generates an message if true.
You Can See Also:
move_uploaded_file()
Handling File Uploads

<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Code of files.php file:

<?php
if ($_FILES["file"] > 0)
{
echo "You have selected a file to upload";
}
?>

PHP $_ENV

Description
$_ENV is used to return the environment variables form the web server.

<?php
<span class="func">echo</span> <span class="vars">$_ENV</span>[<span class="string">'username'</span>];
?>

Example of $_ENV:

<?php
echo "my username is ".$_ENV['username'];
?>

PHP $_COOKIE

PHP Cookie is usually a small piece of data sent from a website and stored in an user’s web browser while an user is browsing a website. One simple route to maintain data between the diverse pages in a web application is with PHP cookies.

<?php
setrawcookie();
print_r($_COOKIE);
?>

PHP : $_SESSION

Sessions are wonderful ways to pass variables. All you need to do is start a session by session_start();Then all the variables you store within a $_SESSION, you can access it form anywhere in the server. Here is an example:

Example of $_SESSION

<?php
session_start();
$_SESSION['w3resource']='The largest online tutorial';
echo $_SESSION['w3resource'];
?>

Advancements in Global Variables:

PHP is rapidly advancing, and as they change the way that it handles data, there is a constant drive to improve the basic security of the system. One such change was to alter a setting called register_globals to default off. What does this mean? Essentially, prior to PHP 4.2.0 you could put together a form, name the input elements, and then just access the information by referring to those names – because they were automatically “registered” globally; the name of the form element was (thanks to php) turned into a variable.

There are some fairly serious security implications. To quote the manual, “By turning off the ability for any user-submitted variable to be injected into PHP code, you can reduce the amount of variable poisoning a potential attacker may inflict. They will have to take the additional time to forge submissions, and your internal variables are effectively isolated from user submitted data.” I won’t go into the details – if you’re interested, you can find out more in the PHP manual online at PHP.org, or just run a search in Google. What this means for you, however, is that any older scripts that you use will no longer work on a server running PHP 4.2.0 or later unless they have explicitly turned register_globals on. You can usually find this out by asking your host.

Add a Comment

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

ABOUT CODINGACE

My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib

CEO: codingace.com

Request a Quote