Forms: GET and POST

A form in the browser sends its values to a PHP script. In PHP the script reads them from the superglobal arrays, checks them, and escapes them before printing anything back.

Read $_GET

Data in the query string lands in the $_GET array. Use it for search, filters, and pagination where the URL should stay shareable. See also How to add variable to url in PHP.

Always assume the key may be missing and provide a default.

The file get.php in the folder you serve with php -S 127.0.0.1:8000:

<?php $q = $_GET["q"] ?? ""; $page = (int)($_GET["page"] ?? 1); echo $q, PHP_EOL; echo $page, PHP_EOL;

curl "http://127.0.0.1:8000/get.php?q=php&page=2"

php
2

The query string fills the two keys, and the cast makes the page number an int whatever the visitor typed. A request without the keys answers with the empty string and the default page of 1.

Read $_POST

Form submissions with method post fill the $_POST array. Use it for logins, comments, and anything that changes data on the server.

Never place passwords or tokens into the query string when post is available.

The file post.php in the same folder:

<?php $email = $_POST["email"] ?? ""; $message = $_POST["message"] ?? ""; echo $email, PHP_EOL; echo $message, PHP_EOL;

curl -d "email=ada@example.com&message=hello" "http://127.0.0.1:8000/post.php"

ada@example.com
hello

The body of the request fills the two keys, and neither value appears in the address bar. The empty default keeps a request without a form from raising a warning.

Validate input

Input from the browser is plain text and cannot be trusted. Check length, format, and allowed values before using it in queries or business rules.

Reject bad data early with a clear message instead of trying to repair it silently.

The file validate.php in the same folder:

<?php $email = trim($_POST["email"] ?? ""); if ($email === "" || strlen($email) > 255) { echo "Email is required"; } elseif (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { echo "Email looks wrong"; } else { echo "Email is ok"; }

curl -d "email=not-an-email" "http://127.0.0.1:8000/validate.php"

Email looks wrong

The value survives the trim and the length check and then fails the format check, so the middle branch answers. A missing field and a valid address take the other two branches.

Escape output with htmlspecialchars

When printing user data back into HTML, escape it with htmlspecialchars(). Without escaping, a value with angle brackets can break markup or inject scripts.

Escape at output time, not at input time, so the stored value stays clean and reusable.

The file escape.php in the same folder:

<?php $name = $_GET["name"] ?? "guest"; echo htmlspecialchars($name, ENT_QUOTES, "UTF-8");

curl "http://127.0.0.1:8000/escape.php?name=%3Cb%3EAda%3C%2Fb%3E"

&lt;b&gt;Ada&lt;/b&gt;

The angle brackets arrive as characters and leave as entities, so the browser shows them as text instead of making the name bold. ENT_QUOTES escapes the single and the double quote as well, which matters when the value lands inside an attribute.

Exercises

  1. Add a sort key to get.php that accepts only name and date and replaces any other value with name.
  2. Send the form of post.php with a missing message and print a dash instead of an empty line.
  3. Request escape.php without the name key. What does the browser show?
  4. Add a length check to validate.php that refuses an address longer than 255 characters before the format check.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Where does data from the query string land?

    Answer

    $_GET

    The part after the question mark is parsed into the $_GET array of the script.

  2. Which method should a login form use?

    Answer

    POST

    A POST body keeps the password out of the address bar, the history and the server logs of the URL.

  3. Why escape a value with htmlspecialchars() before printing it into HTML?

    Answer

    To stop angle brackets and quotes in it from breaking the markup

    The function turns the special characters into entities, so the value shows as text.

  4. Where should escaping happen?

    Answer

    At output time, when the value is printed into HTML

    The stored value stays as the visitor typed it, and each output escapes it for its own context.

  5. What does this program print? <?php $get = ["q" => "php", "page" => "2"]; $q = $get["q"] ?? ""; $page = (int)($get["page"] ?? 1); echo $q; echo $page;

    Answer

    php2

    The two values are echoed one after the other, and the cast turns the page into an int.

  6. What does this program print? <?php $post = ["email" => "not-an-email"]; $email = trim($post["email"] ?? ""); if ($email === "" || strlen($email) > 255) { echo "Email is required"; } elseif (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { echo "Email looks wrong"; } else { echo "Email is ok"; }

    Answer

    Email looks wrong

    The value is present and short, but it is not a valid address, so the format branch answers.

  7. What does this program print? <?php $name = "<b>Ada</b>"; echo htmlspecialchars($name, ENT_QUOTES, "UTF-8");

    Answer

    &lt;b&gt;Ada&lt;/b&gt;

    The angle brackets leave the function as entities, so the browser displays them literally.

  8. What does this program print? <?php $get = ["page" => "7abc"]; $page = (int)($get["page"] ?? 1); echo $page;

    Answer

    7

    An int cast reads the digits at the start of the string and stops at the first letter.

  9. A key is missing from $_GET. What does $_GET["q"] ?? "" give?

    Answer

    The empty string

    The null coalescing operator takes the right side when the key is missing or null, so no warning is printed.

  10. Why is a password in the query string a bad idea?

    Answer

    It stays in the address bar, the browser history and the server logs

    The query string is written down and shared in many places, so a secret in it leaks.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev