Skip to content
PHP Logo

In what ways can PHP developers prevent data manipulation or injection when passing parameters to PHP functions through AJAX requests?

To prevent data manipulation or injection when passing parameters to PHP functions through AJAX requests, developers should sanitize and validate the input data before using it in their code. This can be done by using functions like filter_var() to sanitize input data and by using prepared statements when interacting with databases to prevent SQL injection attacks.

php
// Sanitize and validate input data before using it in PHP code
$param1 = filter_var($_POST['param1'], FILTER_SANITIZE_STRING);
$param2 = filter_var($_POST['param2'], FILTER_VALIDATE_INT);

// Use prepared statements to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM table WHERE param1 = :param1 AND param2 = :param2");
$stmt->bindParam(':param1', $param1);
$stmt->bindParam(':param2', $param2);
$stmt->execute();