This article may contain affiliate links. If you buy some products using those links, I may receive monetary benefits. See affiliate disclosure here
In the previous chapter, we had discussed how you can access global variables using the $GLOBALS superglobal array.
By playing this video, you agree to YouTube's Terms
Apart from that, PHP also offers a couple of other superglobal variables, which can be accessed from anywhere in your code. The following are the ones given in the PHP documentation:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_SESSION
$_COOKIE
$_ENV
Let’s have an overview on each of them, starting with the $_SERVER
variable:
$_SERVER
var_dump($_SERVER);
Some of the array keys in this variable are:
PHP_SELF
– gives the path of the current scriptSERVER_NAME
– gives the name of the server, in this case codelab.localREQUEST_SCHEME
– whether it’s http or https request
$_GET
This array contains the variables passed to the PHP script in the form of query string. Can be used to access HTTP GET request parameters. Consider the following script with an HTML form:
<?php
if(isset($_GET['fname'])) {
echo "The name you entered is: " . $_GET['fname'];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="GET">
<input type="text" name="fname" value="Abhinav">
<input type="submit" value="submit">
</form>
You can see how I used both $_GET
and $_SERVER
arrays in the above code.
$_POST
Similar to $_GET
, $_POST
contains the HTTP POST variables.
<?php
if(isset($_POST['fname'])) {
echo "The name you entered is: " . $_POST['fname'];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type="text" name="fname" value="Abhinav">
<input type="submit" value="submit">
</form>
$_COOKIE
This array contains the cookies sent to the server by the browser.
Try the following in test.php, which sets the cookie in the visitor’s browser:
if (setcookie("site_title", "Coding Reflections") === true) {
echo 'title set successfully';
}
Now, create another file – test-2.php, and open it in browser. You should see the site title value accessed from the cookie.
if(isset($_COOKIE['site_title'])) {
echo 'The site title is: ' . $_COOKIE['site_title'];
}
else {
echo 'Site title not set';
}
Cookies are persistent, so the value remains there even if the browser is closed.
$_SESSION
This variable is similar to cookies, albeit the values get destroyed when the browser is closed. For eg: can be used to save user logins during a session.
To create a new session variable:
session_start();
$_SESSION['user_id'] = 25;
Now try this another PHP file and open in browser:
session_start();
if(isset($_SESSION['user_id'])) {
echo 'you are logged in as: ' . $_SESSION['user_id'];
}
else {
echo 'please login';
}
To unset a session variable:
session_start();
unset($_SESSION['user_id']);
$_REQUEST
The $_REQUEST
variable contains the contents of $_POST
, $_GET
, and $_COOKIE
superglobals.
$_FILES
Contains the files uploaded using an HTML form with multipart/form-data
enctype.
<?php
if(!empty($_FILES['myfile'])) {
var_dump($_FILES);
}
?><form action="<?= $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="myfile" id="myfile">
<input type="submit" value="Upload">
</form>
$_ENV
Environment variables are now a standard and secure way to store sensitive information, such as API keys, database passwords, etc.
The $_ENV
superglobal variable contains the environment variables exposed to the PHP parser by the OS or the web server.
var_dump($_ENV);
However, it should be enabled in the php.ini file, otherwise the variable will be empty.
the getenv()
function is an alternative way to access environment variables, even when the $_ENV
is disabled