Understanding GET and POST Methods, Sessions, and Cookies in PHP

GET vs. POST

GETPOST
  • Data is sent in the URL.
  • Appends name/value pairs to the URL.
  • Limited URL length, suitable for few parameters.
  • Insecure: Parameters are visible in the browser’s address bar.
  • Cannot send binary data (e.g., images, documents).
  • Data is sent within the body of the HTTP request.
  • Packages name/value pairs inside the request body, resulting in a cleaner URL.
  • No size limitations on form output.
  • More secure: Data is passed through the HTTP handler.
  • Can send both ASCII and binary data.
  • Data is accessed using the $_POST superglobal variable.

Sessions in PHP

A session stores information in variables for use across multiple pages.

<?php
session_start();
$_SESSION['username'] = 'abc';
$_SESSION['password'] = '123';
?>
<?php
session_start();
echo "Welcome: " . $_SESSION['username'] . "<br>";
echo "Your Password is: " . $_SESSION['password'];
?>

Cookies in PHP

Cookies are text files stored on the client computer for user tracking. PHP transparently supports HTTP cookies.