Current Location: Home> Latest Articles> Comprehensive Guide to Getting POST Data in PHP

Comprehensive Guide to Getting POST Data in PHP

M66 2025-10-15

Two Methods to Get POST Data in PHP

In PHP development, it is common to retrieve POST data submitted by users through forms. There are two main ways to do this:

Using the $_POST Superglobal

$_POST is a built-in PHP superglobal array that contains all form data submitted via POST requests. You can access POST data like this:

<span class="fun">$value = $_POST['name_of_input'];</span>

Using the php://input Stream

php://input is a read-only input stream that contains the raw POST data. You can retrieve the raw POST data as follows:

<span class="fun">$data = file_get_contents('php://input');</span>

Example Code

The following example demonstrates how to use $_POST to get POST data:

<?php
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['name'];
    $email = $_POST['email'];
    
    echo "Name: $name<br>";
    echo "Email: $email<br>";
  }
?>

Important Notes

  • Make sure the form is submitted using the HTTP POST method.
  • Always validate and sanitize user input to prevent malicious attacks.