Skip to content

Passing Inputs to PHP Scripts

When users interact with your website, they send data to your PHP scripts. This happens when they:

  • Click a link with parameters (like products.php?category=shoes)
  • Submit a form (like a login or contact form)
  • Use a search box

PHP provides special arrays called superglobals to access this data. The two most important ones are $_GET and $_POST. These arrays are automatically populated by PHP with the data the user sent.


$_GET captures data sent through the URL. This data appears after the ? in a web address and is called the query string.

A query string consists of key-value pairs separated by &:

https://example.com/page.php?id=5&name=John&active=true
└─────────────────────────┘
query string

Breaking it down:

  • id=5 → key is “id”, value is “5”
  • name=John → key is “name”, value is “John”
  • active=true → key is “active”, value is “true”

PHP automatically parses the query string and populates the $_GET array:

// URL: page.php?id=5&name=John
$id = $_GET['id']; // "5" (note: it's a string, not a number)
$name = $_GET['name']; // "John"
echo "User $name has ID $id";
  • Pagination: articles.php?page=2
  • Filtering: products.php?category=electronics&sort=price
  • Search: search.php?q=php+tutorials
  • Sharing links: Users can bookmark or share URLs with parameters
  • Data is visible in the URL (don’t use for passwords!)
  • URLs can be bookmarked and shared
  • Limited to about 2000 characters (varies by browser)
  • Data is sent when the page loads - no form needed

$_POST captures data sent through HTML forms when the form’s method is set to “POST”. Unlike GET, this data is sent in the request body, not the URL.

The name attribute on each form input determines the key in $_POST:

// If a form has <input name="username"> and <input name="email">
$username = $_POST['username'];
$email = $_POST['email'];

POST is used for sensitive data (passwords), large data, and actions that modify data (creating accounts, submitting orders).


FeatureGETPOST
Data locationURL query stringRequest body (hidden)
Visible in URLYesNo
BookmarkableYesNo
Data limit~2000 charactersNo practical limit
Browser back buttonSafe to repeatMay warn about resubmission
Saved in browser historyYesNo
Cached by browserYesNo
Logged by serversYes (in access logs)Usually not
Security for sensitive dataNever usePreferred (with HTTPS)
Use forRetrieving/reading dataSubmitting/changing data

Use GET when:

  • Displaying or searching for information
  • The action can be repeated safely (viewing a page twice is fine)
  • You want users to bookmark or share the URL
  • Examples: search results, filtered product lists, pagination

Use POST when:

  • Submitting sensitive information (passwords, personal data)
  • Creating, updating, or deleting data
  • The action should not be accidentally repeated
  • Examples: login, checkout, posting a comment

Think of it this way: GET is like asking a question (“show me product #5”), POST is like giving an instruction (“create this new user”).


$_REQUEST is a superglobal that combines data from both $_GET and $_POST (and $_COOKIE).

// Works whether 'id' came from GET or POST
$id = $_REQUEST['id'];

While convenient, $_REQUEST has drawbacks:

  1. Unclear data source: You can’t tell if the data came from the URL or a form
  2. Security concerns: An attacker might send GET data when you expected POST
  3. Harder to debug: When something goes wrong, you don’t know where to look

Best practice: Always use $_GET or $_POST explicitly. It makes your code clearer and more secure.

// Unclear - where did this come from?
$id = $_REQUEST['id'];
// Clear - this came from the URL
$id = $_GET['id'];
// Clear - this came from a form
$id = $_POST['id'];