What is the best practice for creating a whitelist to validate $_GET parameters in PHP?
When working with user input from $_GET parameters in PHP, it is important to validate and sanitize the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to do this is by creating a whitelist of allowed parameters and only allowing those specific parameters to be used in your application.
// Define a whitelist of allowed parameters
$allowed_params = ['param1', 'param2', 'param3'];
// Check if the parameter passed in the URL is in the whitelist
if (isset($_GET['param']) && in_array($_GET['param'], $allowed_params)) {
// Use the parameter in your application
$param = $_GET['param'];
// Perform any necessary validation or sanitization on $param
} else {
// Handle invalid or unauthorized parameters
echo "Invalid parameter";
}