6 PHP Global Variables
6 PHP Global Variables
1. $GLOBALS
2. $_SERVER
3. $_REQUEST
4. $_POST
5. $_GET
6. $_FILES
7. $_ENV
8. $_COOKIE
9. $_SESSION
1
1. PHP Super global variable: $GLOBALS
$GLOBALS --- is a PHP super global variable which is used to access global variables from anywhere in the
PHP script.
PHP stores all global variables in an array called $GLOBALS[index], where index holds the name of the
variable.
Example:
<?php
$num1 = 100;
$num2 = 251;
getTotal();
echo "Total = $tot";
function getTotal() {
$GLOBALS['tot'] = $GLOBALS['num1'] + $GLOBALS['num2'];
}
?>
$_SERVER[code] ---- is a PHP super global variable which holds information about headers,
paths, and script locations. The code that you can assign to ther $_SERVER include the following:
Code Description
'PHP_SELF' Returns the filename of the currently executing script
'GATEWAY_INTERFACE' Returns the version of the Common Gateway Interface (CGI) the server is using
'SERVER_ADDR' Returns the IP address of the host server
'SERVER_NAME' Returns the name of the host server (such as localhost)
'SERVER_SOFTWARE' Returns the server identification string (such as Apache/2.2.24)
'SERVER_PROTOCOL' Returns the name and revision of the information protocol
'REQUEST_METHOD' Returns the request method used to access the page (such as POST)
'REQUEST_TIME' Returns the timestamp of the start of the request (such as 1377687496)
'QUERY_STRING' Returns the query string if the page is accessed via a query string
'HTTP_ACCEPT' Returns the Accept header from the current request
'HTTP_ACCEPT_CHARSET' Returns the Accept_Charset header from the current request (such as utf-
8,ISO-8859-1)
2
2. PHP Super global variable: $_SERVER
Code Description
'HTTP_HOST' Returns the Host header from the current request
'HTTP_REFERER' Returns the complete URL of the page from which the current page was called
'HTTPS' Is the script queried through a secure HTTP protocol
'REMOTE_ADDR' Returns the IP address from where the user is viewing the current page
'REMOTE_HOST' Returns the Host name from where the user is viewing the current page
'REMOTE_PORT' Returns the port being used on the user's machine to communicate with the web
server
'SCRIPT_FILENAME' Returns the absolute pathname of the currently executing script
'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)
'SERVER_PORT' Returns the port on the server machine being used by the web server for
communication (such as 80)
'SERVER_SIGNATURE' Returns the server version and virtual host name which are added to server-
generated pages
'PATH_TRANSLATED' Returns the file system based path to the current script
'SCRIPT_NAME' Returns the path of the current script
'SCRIPT_URI' Returns the URI of the current page
Example:
<?php
echo $_SERVER['PHP_SELF']. "<br>"; /cs126/05-builtinfunctions/globalvarserver.php
echo $_SERVER['SERVER_NAME']. "<br>"; localhost
echo $_SERVER['SERVER_ADDR']. "<br>"; ::1
echo $_SERVER['HTTP_HOST']. "<br>"; localhost
echo $_SERVER['HTTP_REFERER']. "<br>"; http://localhost/cs126/05-builtinfunctions/
echo $_SERVER['SCRIPT_NAME']. "<br>"; /cs126/05-builtinfunctions/globalvarserver.php
echo $_SERVER['SERVER_PROTOCOL']. "<br>"; HTTP/1.1
echo $_SERVER['HTTP_USER_AGENT']. "<br>"; Mozilla/5.0 (Macintosh; Intel Mac OS X
?> 10_12_6) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/73.0.3683.103 Safari/537.36
3
3. PHP Super global variable: $_REQUEST & $_POST
$_POST -- is widely used to collect form data after submitting an HTML form with method="post".
It is also widely used to pass variables.
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 the following 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, use the super global variable $_REQUEST to collect the value of the input field.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
$email = $_REQUEST['email'];
echo "$name $email";
}
?>
</body>
</html>
4
3. PHP Super global variable: $_REQUEST & $_POST
<!DOCTYPE html>
<html>
<body>
<form method="post" action="receiver.php">
Name: <input type="text" name="fname"> <br>
Email: <input type="text" name="email"> <br>
<input type="submit">
</form>
</body>
</html>
<?php
// Filename: receiver.php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_REQUEST['fname'];
$email = $_REQUEST['email'];
echo "$name $email";
}
?>
Thank you!