How should data passed through $_GET be validated or filtered based on the context in which it will be used?
Data passed through $_GET should be validated and filtered based on the context in which it will be used to prevent security vulnerabilities such as SQL injection or XSS attacks. It is important to sanitize the input data to ensure that only expected and safe values are used in the application.
// Example of validating and filtering data passed through $_GET
$id = isset($_GET['id']) ? intval($_GET['id']) : 0; // Validate and sanitize integer input
$name = isset($_GET['name']) ? filter_var($_GET['name'], FILTER_SANITIZE_STRING) : ''; // Sanitize string input
// Now $id and $name can be safely used in the application
Keywords
Related Questions
- What are the potential pitfalls of using global variables in PHP scripts, as seen in the provided code snippet?
- How can PHP developers ensure the security of their code when interacting with databases through SQL queries?
- What are the potential pitfalls of using arrays in PHP for assigning random values to database entries?