PHP Superglobals for Form Handling

🌍 What Are Superglobals in PHP?

Superglobals are built-in predefined variables in PHP that:

🧺 List of Common PHP Superglobals

Superglobal What It Does
$_GETData from URL parameters
$_POSTData from submitted forms
$_REQUESTCombines $_GET, $_POST, and $_COOKIE
$_SERVERInfo about headers, server, paths, etc.
$_FILESData from uploaded files
$_ENVEnvironment variables
$_COOKIEData stored in cookies
$_SESSIONData in user sessions
$GLOBALSAll global variables in one place

PHP Global Variables - Superglobals

Secure PHP Form Handling wit htmlspecialchars()

What htmlspecialchars() Does?

GET vs. POST

The PHP superglobals $_GET and $_POST are used to collect form-data.

$_POST

	<!-- in test.html -->
	<!DOCTYPE html>
	<html>
	<body>
	
	<form action="test.php" method="post">
	Name: <input type="text" name="name"  > 
	<br>
	E-mail: <input type="text" name="email">
	<br>
	
	<input type="submit">
	</form>
	
	</body>
	</html>
When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "test.php". The form data is sent with the HTTP POST method.
	<!-- in test.php -->
	<html>
	<body>
	
	Welcome <?php echo $_POST["name"]; ?><br>
	Your email address is: <?php echo $_POST["email"]; ?>
	
	</body>
	</html>

$_GET

	<!-- in test.html -->
	<html>
	<body>
	
	<form action="test.php" method="get">
	Name: <input type="text" name="name"><br>
	E-mail: <input type="text" name="email"><br>
	<input type="submit">
	</form>
	
	</body>
	</html>
	<!-- in test.php -->
	<html>
	<body>
	
	Welcome <?php echo $_GET["name"]; ?><br>
	Your email address is: <?php echo $_GET["email"]; ?>
	
	</body>
	</html>
βœ… Use GET when... βœ… Use POST when...
Sending non-sensitive data Sending sensitive or large data
Bookmarking/sharing a URL Submitting forms that update data
You want URL parameters visible You want data hidden from the URL

Reference