HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

PHP GET & POST

Both GET and POST are methods used to send data from a client (browser) to a server. They are handled by PHP using the $_GET and $_POST superglobal variables.


1. The GET Method

GET sends data as part of the URL. This is known as a Query String. Because the data is in the URL, it is visible to everyone and has a length limit (around 2000 characters).

Example URL with GET:

test_get.php?subject=PHP&web=redohub.com

Accessing GET Data:

<?php
    echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>

2. The POST Method

POST sends data inside the HTTP request body. It is invisible in the URL and has no size limit. It is the standard for submitting forms with passwords or large amounts of text.

<?php
    echo "Hello " . $_POST['username'];
?>

3. Comparison Summary

Feature GET POST
VisibilityVisible in URLHidden
SecurityLow (don't use for pwd)High
BookmarksCan be bookmarkedCannot be bookmarked
Data Limit~2KBNo limit
Use CaseRetrieving data, searchSensitive info, file uploads
Safety Reminder: $_GET is accessible via URL parameters, so users can easily manipulate the values. Always validate and sanitize inputs.
The $_REQUEST Superglobal: PHP also provides $_REQUEST, which contains the contents of both $_GET and $_POST. However, using the specific method is generally better for clarity and security.

Key Takeaways

  • Use GET for data that won't change server state (like search filters).
  • Use POST for data that modifies state or contains sensitive info.
  • GET data can be cached and bookmarked; POST data cannot.
  • Both methods require careful input sanitization to prevent security vulnerabilities.