DEV Community

Cover image for PHP input validation yang benar
Eko Priyanto
Eko Priyanto

Posted on • Edited on

PHP input validation yang benar

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

Top comments (0)

AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️