Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

PHP Superglobals $_GET, $_POST, $_SESSION: Tutorial, Examples, FAQs & Interview Tips

Superglobals are built-in variables that are always accessible regardless of scope - inside functions, classes, or files. They are prefixed with $_.

$_GET and $_POST

$_GET collects data sent via URL query strings. $_POST collects data sent via HTTP POST (form submissions). $_REQUEST contains both.

$_GET and $_POST
<?php
// URL: example.com/page.php?name=Alice&age=30
$name = $_GET['name'] ?? 'Guest';
$age  = $_GET['age']  ?? 0;
echo "Name: $name, Age: $age";

// POST data from a form
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = htmlspecialchars($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    echo "Username: $username";
}

// $_REQUEST combines GET, POST, and COOKIE
$search = $_REQUEST['q'] ?? '';
echo "Search: $search";
?>

$_SERVER

$_SERVER contains information about the server environment, headers, and the current request.

$_SERVER
<?php
echo $_SERVER['SERVER_NAME'];    // e.g. localhost
echo $_SERVER['HTTP_HOST'];      // e.g. www.example.com
echo $_SERVER['SCRIPT_NAME'];    // e.g. /index.php
echo $_SERVER['REQUEST_METHOD']; // GET or POST
echo $_SERVER['REMOTE_ADDR'];    // visitor's IP address
echo $_SERVER['HTTP_USER_AGENT']; // browser info
echo $_SERVER['QUERY_STRING'];   // everything after ?
echo $_SERVER['DOCUMENT_ROOT'];  // server root path
echo $_SERVER['PHP_SELF'];       // current script path
?>

$_FILES, $_ENV, $_COOKIE, $_SESSION

Other Superglobals
<?php
// $_FILES - file upload info
// <input type="file" name="photo">
$fileName = $_FILES['photo']['name']     ?? '';
$fileSize = $_FILES['photo']['size']     ?? 0;
$fileTmp  = $_FILES['photo']['tmp_name'] ?? '';
$fileErr  = $_FILES['photo']['error']    ?? 1;

if ($fileErr === UPLOAD_ERR_OK) {
    move_uploaded_file($fileTmp, "uploads/$fileName");
    echo "Uploaded: $fileName";
}

// $_ENV - environment variables
$dbHost = $_ENV['DB_HOST'] ?? 'localhost';

// $_COOKIE - read a cookie
$theme = $_COOKIE['theme'] ?? 'light';
echo "Theme: $theme";

// $_SESSION - read session data
session_start();
$_SESSION['user'] = 'Alice';
echo $_SESSION['user']; // Alice
?>
Key Takeaways
  • PHP superglobals are built-in variables available in all scopes without needing global keyword.
  • Always sanitize and validate data from \, \, and \ before using it.
  • Use filter_input() or filter_var() to safely retrieve and validate superglobal values.
  • \ requires session_start() to be called before use - call it at the top of every page.
  • \ contains information about uploaded files - always validate file type and size.
  • Never trust \ values directly - they can be manipulated by the client.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.