PHP input validation yang benar
Eko Priyanto

Eko Priyanto @ekopriyanto

About: Eko Priyanto

Location:
Indonesia
Joined:
Nov 27, 2020

PHP input validation yang benar

Publish Date: Jan 31
0 0

never trust your user

// Wrong way ❌
$userId = $_GET['user_id'];
$query = "SELECT * FROM users WHERE id = " . $userId;

// Right way ✅
$userId = filter_input(INPUT_GET, 'user_id', FILTER_VALIDATE_INT);
if ($userId === false) {
    throw new InvalidArgumentException('Invalid user ID');
}
$query = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$query->execute([$userId]);
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment