PHP Superglobal Variables and It's Practical Examples - Get, Post, Session, Cookie, Request, Server

PHP Superglobal Variables and It's Practical Examples - Get, Post, Session, Cookie, Request, Server

PHP Superglobal Variables

PHP has some built in variables which are accessible to everywhere regardless of scopes. This variables can be accessed everywhere. Let’s get some list of super global variables in PHP.

Name Description Example
$GLOBALS Assign or access any variable data globally and not limited for any scope. $GLOBALS['x'] = 40;
$_POST Global Post variable is used after submitting a form which method type is POST. $_POST['name']
$_GET Global Get variable is used after submitting a form which method type is GET. $_GET['search']
$_REQUEST Global Request variable is used for both GET or POST. $_REQUEST['search']
$_SERVER Global all server data can be easily accessed by this $_SERVER['HTTP_HOST'] // Get Host Name
$_COOKIE Cookie is used to store logged in user informations. Every time, a user access a web page, Server embeds a little file in Users browser. That is the Cookie. Cookie could be set upto defined time. setcookie($cookie_name, $cookie_value, $expires_at);
$_SESSION Session is used upto one time browser tab close. It's stored in Server and not in Users computer or browser. $_SESSION["name"] = "Akash";

$GLOBALS

Access any variable within any scope or function or method. All of the variables outside a function or a scope are Global variables by default in PHP.

function test()
{
    echo "Global Scope Data:: " . $GLOBALS['x'] . "<br>"; // We can still access this
    $x = 12 + $GLOBALS['x'];
    // $GLOBALS['x'] = $x;
    $z = 20;
    echo "Inside Function:: " . $x . "<br>";
}

$x = 100;
$y = 11;
echo "Outside Function:: " . $x . "<br>";

test();

print_r($GLOBALS);

$_POST and $_GET

$_POST and $_GET is used for form submission. More secure is to use $_POST. Let’s get a form example with Post submission. Important Requirement is to give every input a name attribute.

Access all variables of $_POST and $_GET

$_POST Post is mainly use for normal form submission.

print_r($_POST);

$_GET Get method is used for URL based form submission, like Search.

print_r($_GET);

Full Form Submission Example with Validation with PHP

Let’s make a Contact Us form and submit that.

Give method="POST" and action="" or action="<?php echo $_SERVER['PHP_SELF']; ?>". Like so -

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<!-- Codes -->
</form>

And Every Input would like this here name field is mandatory for PHP submission.

<label for="name">Name</label><br>
<input type="text" id="name" name="name"> <br>
<span style="color: red"><?php echo empty($_POST['name']) ? "Please give your name" : '' ?></span>
<br>

Full Form Example -

<h2> Contact Us </h2>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <?php
    if (!empty($_POST['name']) && !empty($_POST['subject']) && !empty($_POST['temp_password'])) {
        echo "<p>Your form is submitted !</p>";
    }
    ?>

    <label for="name">Name</label><br>
    <input type="text" id="name" name="name"> <br>
    <span style="color: red"><?php echo empty($_POST['name']) ? "Please give your name" : '' ?></span>
    <br>

    <label for="subject">Subject</label><br>
    <input type="text" id="subject" name="subject"> <br>
    <span style="color: red"><?php echo empty($_POST['subject']) ? "Please give subject" : '' ?></span>
    <br>

    <label for="temp_password">Temp Password</label><br>
    <input type="password" id="temp_password" name="temp_password"> <br>
    <span style="color: red"><?php echo empty($_POST['temp_password']) ? "Please give a temporary password to contact with you" : '' ?></span>
    <br>

    <input type="submit" value="Send" />
</form>

$_SERVER

To get access and information about servers related data this super global variables are used.

Get all server variables in this script -

print_r($_SERVER);

Get any server data with index access.

<?php
    echo '<br>Host Name:' . $_SERVER['HTTP_HOST'];
    echo '<br>Browser Name:' . $_SERVER['HTTP_USER_AGENT'];
    echo '<br>Server IP Address:' . $_SERVER['SERVER_ADDR'];
    echo '<br>Server Port:' . $_SERVER['SERVER_PORT'];
    echo '<br>Current File Full Path:' . $_SERVER['PHP_SELF'];
    echo '<br>REQUEST_METHOD:' . $_SERVER['REQUEST_METHOD'];
?>

$_COOKIE

Cookie is stored in User’s browser with a specific time limit.

Get all cookie variables in this script -

print_r($_COOKIE);

Create a Cookie -

setcookie(cookie_name, cookie_value, expired_at, path); function is used to create a cookie.

setcookie("user_name", "Jhon", time() + 60*60*24, "/");

Update a Cookie -

Same setCookie() function is used to update the cookie

setcookie("user_name", "Jhon Doe", time() + 60*60*24, "/");

Delete or Unset a Cookie -

Same setCookie() function is used to unset a cookie, just use a time less than current time.

setcookie("user_name", "Jhon Doe", time() - 60, "/");

$_SESSION

Session is stored in the Server. It’s better than Cookie because, if stored in cookie, user can see the data and can manipulate easily. But session is stored in the server and so user or client can not modify that easily. In server, the session is stored in a temporary file.

To work with session, first line of your file would be session_start(); -

session_start();

All Sessions data would be stored in $_SESSION -

print_r($_SESSION);

Create Session and store a value

Let’s start session and store user_name value “Jhon Doe”

session_start();

// Create a session
$_SESSION['user_name'] = "Jhon Doe";

Update Session value

Our Previously stored user_name value would be updated with Akash -

$_SESSION['user_name'] = "Akash";

Delete, destroy session -

session_unset();
session_destroy();
Previous
PHP Loosely Type Language - Strict type from PHP 7
Next
Form Submission and File Upload in PHP - Basic to Advance Level Form Submission in PHP