unknown time to read, 1.78K views since 2016.10.09

How superglobal variables can help you

What means superglobal variable in PHP?

Every scropt in php gets some data from web server on load. You can access this data via so called superglobal variables.

PHP assigns 6 superglobal arrays that can be accessed in every script:

$_POST,$_GET,$_COOKIE,$_SERVER, $_REQUEST and `$_SESSION.

They called superglobal because they can be accessed in every place of the php code and even functions without declaring them as global. You ca imagine them as if they were initialized right before you use them, so they're always available to you.

Lets get a quick review over each type of a superglobals

$_POST holds data passed to script via POST http method.

For example: if you send a post form in html with input with name equals to login you can access it in a script by post superglobal array:

If($_POST['login'] === 'admin') {
    echo "hello, admin!";
}

post superglobal array along with the get is one of the most used superglobal variables in php scripts.

$_GET carries parameters from query string

You can get variables provided to script by query string by simply getting proper array index. For exampe you have a page on your website, path to which is next: site.com/page.php?alias=somealias:

<?php

echo $_GET['alias'];

Attention! Before using any variable from superglobal array you should check if its exists ( in case of our example if the parameter alias are in the uri). It can be done with the help of isset:

<?php 

if (isset($_GET['alias'])){
    echo 'the alias is '.$_GET['alias'];
} else {
    echo 'Error: alias parameter wasn\'t provided';
} 

cookies and $_COOKIE superglobal. $_SESSION

All cookies which are bind to current page could beaccessed via $_COOKIE variable.

Lets revise example of cookie-based authorization in php:


If  (isset($_COOKIE['login']) && isset($_COOKIE['password'])){
    if(checkLoginPassword($_COOKIE['login'],$_COOKIE['password'])) { // some function which checks if the password for login is correct
        session_start(); // starts session handling for current page
        $_SESSION['authorized'] = true;
    }
}

REQUEST superglobal

Variables which can be accessed in either post or get superglobals could be acessed by the $_REQUEST variable also.

This variable merges content of get and post superglobals, so it easier to, reach some data.for example you don't know how user provides its data - via post or get method, or, in other case you allow to provide data via get and post for user's convinience.

Most interesting is $_SERVER superglobal array

The server superglobal variable contains much interesting things about the person who request your page.

Here's how you can client's ip address in php:

<?php

echo 'hi there, your ip is '.$_SEREVER['REMOTE_ADDR'];

Based on client's ip you can determine his geo position, country etc. - data which you can use to show proper content for the client. Be careful, on local environment the client's ip would be always 127.0.0.1.

How to get client user agent (browser name) in php

You can easily acess client's user agent line from php using server superglobal:

<?php

echo $_SERVER['HTTP_USER_AGENT'];

Getting a referer url

Using server superglobal you can get a page, from which a client gets to your page.

Use $_SERVER['HTTP_REFERER'] to get this data.

getting a current website name (hostname)

Consider situation in which your script don't know on what website it is running now. It can be a situation when you need to generate a link whicb includes site's domain name:


// prints current website domain name

echo $_SERVER['HTTP_HOST'];

There are lot of things you can get from the server superglobal. You can review all the data which it vonsists by the following construction:

<?php

print_r($_SERVER);
Read next article Working with files and folders in course Basic PHP