What is a proper way to filter parameters passed in functions? The goal is to make the function secure, especially when working with a database.
Example:
function user_profile($user_id)
{
//get user's profile data
$query = "SELECT * FROM `users` WHERE `user_id` = $user_id";
}
$user_id is a URI segment.
Other general examples are welcomed.
To escape strings, use the same method you’d use outside the function:
If you’re expecting the value to be, for example, an integer and would like to return error from the function if it isn’t, you can do something like:
Or if you expect it to match some specific pattern, do so with
preg_match():