What are some best practices for handling variables passed through URLs in PHP classes?
When handling variables passed through URLs in PHP classes, it is important to sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to achieve this is by using PHP's filter_input() function to retrieve and filter the input data from the URL parameters.
class User {
private $userId;
public function __construct() {
$this->userId = filter_input(INPUT_GET, 'user_id', FILTER_SANITIZE_NUMBER_INT);
}
public function getUserId() {
return $this->userId;
}
}
$user = new User();
echo $user->getUserId();
Keywords
Related Questions
- How can PHP be optimized to efficiently handle the automatic progression of Powerpoint slides in a browser without causing delays or interruptions?
- What are common methods in PHP to check if a date is in the future or past?
- What are the potential implications of not using backticks in PHP MySQL queries?