What is the $_GET and $_POST Superglobal used for in PHP?
Last Updated :
19 Feb, 2024
In PHP, $_GET
and $_POST
are superglobal arrays used to retrieve data sent to the server using the GET and POST methods, respectively, in an HTTP request.
$_GET
:
- Retrieves data sent to the server as part of the URL query string.
- Useful for retrieving data that is visible to the user, such as search queries or parameters in a URL.
- Data sent via
$_GET
is visible in the URL, making it less secure for sensitive information.
- Accessed using associative array notation, e.g.,
$_GET['parameter']
.
Syntax:
Accessing data from $_GET
:
// Assuming a URL like: http://example.com/page.php?parameter=value
if(isset($_GET['parameter'])) {
$parameterValue = $_GET['parameter'];
// Use $parameterValue as needed
}
$_POST
:
- Retrieves data sent to the server in the body of an HTTP request, typically from HTML forms submitted using the POST method.
- Suitable for handling sensitive or large amounts of data as it’s not visible in the URL.
- More secure than
$_GET
for handling sensitive information like passwords.
- Accessed using associative array notation, e.g.,
$_POST['field_name']
.
Syntax:
Accessing data from $_POST
:
// Assuming a form with method="post" submitted to this PHP script
if(isset($_POST['field_name'])) {
$fieldValue = $_POST['field_name'];
// Use $fieldValue as needed
}
It’s essential to validate and sanitize data obtained from $_GET
and $_POST
to prevent security vulnerabilities like SQL injection or cross-site scripting (XSS) attacks. Additionally, $_REQUEST
is another superglobal array that combines $_GET
, $_POST
, and $_COOKIE
data, but its usage is generally discouraged due to potential security risks and ambiguity.
Share your thoughts in the comments
Please Login to comment...