In PHP , Superglobals are specially-defined array variables that make it easy for you to get information about a request or its context. They are called superglobal because they are always accessible, regardless of the scope — that is, we can access them from any function, class or file without having to do anything special.
The PHP superglobal variables are:
1. $GLOBALS
$GLOBAL is a PHP super global variable. It is using to access global variables from anywhere in the PHP script (also from within functions or methods).
$a = 1;
$b = 2;
function Sum()
{
$GLOBALS[‘b’] = $GLOBALS[‘a’] + $GLOBALS[‘b’];
}
Sum();
echo $b;
2. $_SERVER
$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.
// The filename of the currently executing script.
echo $_SERVER[‘PHP_SELF’];
// The name of the server host under which the current script is executing.
echo $_SERVER[‘SERVER_NAME’];
// Contents of the Host: header from the current request, if there is one.
echo $_SERVER[‘HTTP_HOST’];
// The address of the page (if any) which referred the user agent to the current page. This
echo $_SERVER[‘HTTP_REFERER’];
// Contents of the User-Agent: header from the current request, if there is one.
echo $_SERVER[‘HTTP_USER_AGENT’];
// Contains the current file name and script’s path.
echo $_SERVER[‘SCRIPT_NAME’];
3. $_REQUEST
PHP $_REQUEST is used to collect data after submitting an HTML form.
$name = $_REQUEST[‘user_name’];
echo $name;
4. $_POST
It is using to collect form data after submitting an HTML form with method=”post”. $_POST is also widely used to pass variables.
$name = $_REQUEST[‘user_name’];
echo $name;
5. $_GET
It can also be used to collect form data after submitting an HTML form with method=”get”.
$name = $_REQUEST[‘user_name’];
echo $name;
6. $_FILES
PHP $_FILES array you can upload files from a client computer to the remote server.
The first parameter is the form’s input name and the second index can be either “name”, “type”, “size”, “tmp_name” or “error”. Like this:
$_FILES[“file_field”][“name”] – the name of the uploaded file
$_FILES[“file_field”][“type”] – the type of the uploaded file
$_FILES[“file_field”][“size”] – the size in bytes of the uploaded file
$_FILES[“file_field”][“tmp_name”] – the name of the temporary copy of the file stored on the server
$_FILES[“file_field”][“error”] – the error code resulting from the file upload
// / Data from an HTML Form, input file field name is “file_field”. In form enctype=”multipart/form-data” need to be added.
if ($_FILES[“file_field”][“error”] > 0)
{
echo “Error: ” . $_FILES[“file_field”][“error”] . “< br>“;
}
else
{
echo “Upload: ” . $_FILES[“file_field”][“name”] . “< br>“;
echo “Type: ” . $_FILES[“file_field”][“type”] . “< br>“;
echo “Size: ” . ($_FILES[“file_field”][“size”] / 1024) . ” kB< br>“;
echo “Stored in: ” . $_FILES[“file_field”][“tmp_name”];
}
7. $_ENV
An associative array of variables passed to the current script via the environment method.
8. $_COOKIE
It is to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
// Store cookie data
setcookie(“user_name”, “Kiran Deepu”, time()+3600);
// Retrieve cookie data
echo $_COOKIE[“cookie”];
9. $_SESSION
It is using to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.
session_start();
// Store session data
$_SESSION[‘views’]=1;
// Retrieve session data
echo “Page Visits=”. $_SESSION[‘views’];