Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials PHP Other Superglobals
PHP Intermediate FREE

Other Superglobals

Lesson 2 of 17 Intermediate Interactive

PHP has several superglobal variables that are always available in every scope. They provide access to server information, files, cookies, sessions, and environment variables.

PHP Superglobals

  • $_SERVER — Server and execution environment information (IP, user agent, request method)
  • $_FILES — File upload information (name, type, size, tmp_name)
  • $_COOKIE — Cookies sent by the browser
  • $_SESSION — Session data (requires session_start())
  • $_ENV — Environment variables

Tip: Superglobals are always available — no need to declare them with global keyword.

Syntax

PHP
<?php
// Server info

echo $_SERVER["HTTP_HOST"];
echo $_SERVER["REQUEST_METHOD"];
echo $_SERVER["REMOTE_ADDR"];

// Cookies

setcookie("user", "Sukhwinder", time() + 86400);
echo $_COOKIE["user"];

// Sessions

session_start();
$_SESSION["user_id"] = 42;
$_SESSION["username"] = "Sukhwinder";

// File uploads

if ($_FILES["avatar"]["error"] === UPLOAD_ERR_OK) {
    move_uploaded_file(
        $_FILES["avatar"]["tmp_name"],
        "uploads/" . $_FILES["avatar"]["name"]
    );
}

// Environment

echo $_ENV["APP_ENV"];
?>
PHP Superglobals
PHP
<?php
// $_GET and $_POST (form data)
// $_SERVER (server info)
echo "Method: " . $_SERVER["REQUEST_METHOD"] . "
";
echo "URL: " . $_SERVER["REQUEST_URI"] . "
";

// $_ENV
echo "PHP Version: " . PHP_VERSION . "
";

// $_FILES (file uploads)
// $_SESSION (sessions)
// $_COOKIE (cookies)

// Useful constants
echo "Document Root: " . $_SERVER["DOCUMENT_ROOT"] . "
";
?>

Practice

1
Exercise

Set a cookie called "last_visit" that stores the current date and lasts for 30 days.

Answer
<?php
setcookie("last_visit", date("Y-m-d"), time() + (86400 * 30));
echo "Cookie set! Visit again within 30 days.";
?>
2
Exercise

Create a simple page view counter using $_SESSION.

Answer
<?php
session_start();
$_SESSION["views"] = ($_SESSION["views"] ?? 0) + 1;
echo "Page views: " . $_SESSION["views"];
?>

Quick Quiz

1

Which superglobal stores session data?

$_SESSION stores data that persists across page loads for a specific user.

2

What must you call before using $_SESSION?

session_start() must be called before any session operations.

3

Which superglobal contains information about uploaded files?

$_FILES contains all information about files uploaded via HTML forms.

Interview Questions

When session_start() is called, PHP generates a unique session ID and stores it in a cookie on the client. The session data is stored on the server (in files or a database). Each time the user makes a request, the session ID is sent back, and PHP loads the corresponding session data from $_SESSION. Sessions are used to maintain user state across page loads.

Cookies are stored on the client (browser) and sent with every request — limited to ~4KB and expire after a set time. Sessions are stored on the server and only the session ID is stored in a cookie. Sessions are more secure (data not visible to users) and can store unlimited data, but require server resources.