What is the best practice for retrieving variables passed through a link using $_GET in PHP?
When retrieving variables passed through a link using $_GET in PHP, it is best practice to sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to do this is by using PHP's filter_input() function with the FILTER_SANITIZE_STRING or FILTER_SANITIZE_NUMBER_INT filter flags.
// Retrieve and sanitize the 'id' variable passed through a link using $_GET
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
// Check if the 'id' variable is set and is a valid integer
if(isset($id) && is_numeric($id)) {
// Process the sanitized 'id' variable
echo "ID: " . $id;
} else {
// Handle invalid input or display an error message
echo "Invalid ID";
}
Related questions
-
PHP variable scope naming conflicts
-
empty isset is_null
-
PHP mail function common errors resolution