About
In this code snippet, we’ll learn how to get HTTP request variables in PHP.
When an HTTP request is made to the PHP backend the data associated with the request(POST data, GET url parameters, cookies, …) will get stored as superglobal variables available from everywhere in the code. Here we’ll see how to access some of these values.
Let’s see the example code below.
Code:
<?php //POST////////////////////////////////// //For the following form ... //<form> // <input type="text" name="myvar"> // <input type = "submit" /> //</form> // ... you can get the form data from POST request like so: $myVar = $_POST["myvar"]; //If you are sending some data like json in the body of the POST reqeust you can get it like so: $postBodyData = file_get_contents('php://input'); //////////////////////////////////////// //GET/////////////////////////////////// //Get url parameters from GET request. //Example url: http://localhost/Code%20Examples/php/request%20variables/?myvar=Hello&mysecondvar=World $myVar = $_GET["myvar"]; //value: "Hello" $mySecondVar = $_GET["mysecondvar"]; //value: "World" //////////////////////////////////////// //Cookies/////////////////////////////// //Get any cookies sent with the request by their name. $cookie = $_COOKIE["mycookie"]; ////////////////////////////////////////