PHP Request Variables

Code Snippets Request Variables
Share:

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"];
////////////////////////////////////////
Share:

Leave a Reply

Your email address will not be published.

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger