PHP Reference ยท Variables and Constants
PHP $_POST superglobal
Reference for PHP $_POST: read form fields sent in a request body, checkboxes, array fields and JSON bodies, and validate and escape every value.
$_POST is an associative array of the fields sent in the body of an HTTP POST request when the content type is application/x-www-form-urlencoded or multipart/form-data. It is what an ordinary HTML form with method="post" produces.
$_POST stays empty for other body types. A request with Content-Type: application/json is not parsed into $_POST; read the raw body with file_get_contents("php://input") and decode it yourself. Like all request data, every value is a string (or an array of strings) chosen by the client and must be validated.
<?php if ($_SERVER["REQUEST_METHOD"] === "POST") { $email = trim($_POST["email"] ?? ""); }
Try it
A command-line script has no request, so these examples fill $_POST by hand, exactly as PHP would after parsing a submitted form.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
$_POST $_POST["field"] $_POST["field"][]
| Part | Description |
|---|---|
$_POST |
Superglobal array. Empty on GET requests and for bodies that are not form-encoded. |
| Keys | Field names from the name attributes of the form controls. |
| Values | Strings, or nested arrays for name="items[]" and name="user[email]". |
Type: array<string|int, string|array>
Usage notes
- Check the method, not just the array.
if ($_POST)is false for an empty form and for aGET. Test$_SERVER["REQUEST_METHOD"] === "POST"to decide whether a submission happened. - Unchecked checkboxes are absent. Use
isset($_POST["agree"]). Unselected radio groups are also missing. - Missing keys warn. Read with
??to avoidUndefined array keywarnings. - Buttons. A submit button with a
nameis included only if it was the button used. Check it withisset($_POST["save"])when a form has several buttons. - JSON APIs. Read
php://inputand usejson_decode().$_POSTstays empty forapplication/json. - Validation is not optional.
maxlength,requiredandtype="email"in HTML are conveniences; a script can send anything. Validate on the server, then escape output withhtmlspecialchars(). - Protect state-changing forms with a CSRF token. See CSRF protection.
- Limits.
post_max_size(default 8M) andmax_input_vars(default 1000) apply. If a body is larger thanpost_max_size, PHP leaves$_POSTand$_FILESempty without an error message in your code. - File fields. Uploaded files are in
$_FILES, not in$_POST; only the other text fields of a multipart form land here. - Post/Redirect/Get. After a successful submission, redirect with
header()so a page refresh does not send the form again. - Prefer a filter for typed input.
filter_input(INPUT_POST, ...)reads and validates in one step.
Related
$_GET,$_REQUEST,$_FILES,filter_var(),filter_input()- Lessons: Forms, Form validation, CSRF