PHP $_REQUEST – Complete Guide with Examples

Introduction

The $_REQUEST variable is a superglobal array in PHP that collects data from multiple sources including $_GET, $_POST, and $_COOKIE. It allows developers to access user input without worrying about the method used to send the data.

For example, when a user submits a form or when data is passed through the URL, PHP stores the values in different arrays depending on the request method. Instead of checking each array separately, developers can use $_REQUEST to access the values from one place.

What is PHP $_REQUEST?

In PHP, $_REQUEST is a superglobal associative array that contains data from $_GET, $_POST, and $_COOKIE variables.

This means that whenever a user sends data through a form or a URL, PHP stores that information in the corresponding array. The $_REQUEST variable gathers all this data and makes it accessible through a single array.

For example, if a form sends a field named username, the value can be accessed using:


$_REQUEST['username']

This will work regardless of whether the data was sent using the GET method or the POST method.

Why It Is Used

The $_REQUEST variable is used because it provides a convenient way to access user input from different sources.

Here are some common reasons why developers use $_REQUEST:

1. Access Data from Multiple Methods

Developers can retrieve data sent via GET, POST,

or COOKIE without checking each variable separately.

2. Simplifies Code

Instead of writing multiple conditions to check whether data is in $_GET or $_POST, developers can directly use $_REQUEST.

3. Flexible Form Handling

Some applications allow forms to be submitted using either GET or POST, and $_REQUEST can handle both cases.

4. Easy Data Access

It provides a simple and unified way to access user input.

5. Useful for Small Applications

In simple scripts or testing environments, $_REQUEST can reduce the amount of code needed to retrieve form values.

However, in large or secure applications, developers usually prefer $_GET or $_POST explicitly for better control.

Syntax

The syntax for using $_REQUEST in PHP is straightforward.

Basic Syntax


$_REQUEST['variable_name']

Example:


$name = $_REQUEST['name'];

HTML Form Example:


<form method="post" action="welcome.php">
 Name: <input type="text" name="name">
 <input type="submit" value="Submit">
</form>

PHP Script


$name = $_REQUEST['name'];
echo "Hello " . $name;

Explanation:

  • The form sends the value of name.
  • PHP stores the value in $_POST.
  • The $_REQUEST variable retrieves the value regardless of the method used.

Example

Below is a simple example demonstrating how $_REQUEST works.

HTML Form


<form method="get" action="result.php">
 Enter your country:
 <input type="text" name="country">
 <input type="submit" value="Submit">
</form>

PHP File (result.php)


$country = $_REQUEST['country'];
echo "You are from " . $country;

Explanation:

  1. The user enters a country name in the form.
  2. The form sends the data using the GET method.
  3. PHP stores the value in $_GET.
  4. The $_REQUEST array retrieves the value and displays the result.

Even if the form used the POST method, the same code would still work.

Real-Life Example

A real-life example of $_REQUEST can be seen in a simple search system.

Suppose a website allows users to search for articles. The search keyword may be passed through the URL or through a form submission.

Example URL:


website.com/search.php?keyword=php

PHP script:


$keyword = $_REQUEST['keyword'];
echo "Searching for: " . $keyword;

The website can then use the keyword to retrieve matching articles from the database.

Another example is a feedback form where users submit their comments. The website may accept the input using either GET or POST methods, and $_REQUEST allows developers to retrieve the data easily.

Common Mistakes

Beginners often make several mistakes when using $_REQUEST.

1. Using $_REQUEST Without Validation

Developers sometimes trust user input without validating it. This can cause security problems.

2. Not Knowing the Source of Data

Since $_REQUEST combines GET, POST, and COOKIE data, it may be difficult to determine where the value actually came from.

3. Security Risks

Attackers might manipulate cookies or URL parameters, which could affect the data stored in $_REQUEST.

4. Using $_REQUEST in Secure Applications

For login systems or payment systems, it is better to use $_POST directly instead of $_REQUEST.

5. Not Checking If the Variable Exists

If a variable is not set, it may cause warnings.

Example solution:


if(isset($_REQUEST['name'])){
  $name = $_REQUEST['name'];
}

Conclusion

The $_REQUEST superglobal variable in PHP provides a convenient way to access data sent from users through GET, POST, or COOKIE methods. It simplifies form handling by allowing developers to retrieve input values without checking multiple arrays.

However, because $_REQUEST combines different data sources, it should be used carefully, especially in applications that require strict input handling. Developers should always validate and sanitize user input to ensure security and reliability.

Understanding how $_REQUEST works helps developers write flexible PHP scripts and handle user input efficiently.

Related PHP Tutorials