0
0
PHPprogramming~10 mins

Why security is critical in PHP - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a PHP session, which is important for managing user security.

PHP
<?php
session_[1]();
?>
Drag options to blanks, or click blank then click option'
Aopen
Bstart
Cinit
Dbegin
Attempts:
3 left
💡 Hint
Common Mistakes
Using session_open() or session_init() which do not exist.
Forgetting to start the session before using session variables.
2fill in blank
medium

Complete the code to safely get a value from user input to prevent security risks.

PHP
<?php
$user_input = filter_input(INPUT_GET, '[1]', FILTER_SANITIZE_STRING);
?>
Drag options to blanks, or click blank then click option'
Aemail
Bpassword
Cusername
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Not sanitizing user input which can lead to security vulnerabilities.
Using incorrect input names that do not match the form.
3fill in blank
hard

Fix the error in the code to prevent SQL injection by using prepared statements.

PHP
<?php
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
$stmt->execute(['[1]' => $email]);
$results = $stmt->fetchAll();
?>
Drag options to blanks, or click blank then click option'
Aemail
Bemail_address
Cuser_email
Dmail
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different key than the placeholder causes errors.
Not using prepared statements leads to SQL injection risks.
4fill in blank
hard

Fill both blanks to create a secure password hash and verify it correctly.

PHP
<?php
$hash = password_[1]($password, PASSWORD_DEFAULT);
if (password_[2]($password, $hash)) {
    echo 'Password is valid';
}
?>
Drag options to blanks, or click blank then click option'
Ahash
Bverify
Ccheck
Dencrypt
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names like password_encrypt or password_check.
Not verifying the password properly.
5fill in blank
hard

Fill all three blanks to safely escape output and prevent cross-site scripting (XSS).

PHP
<?php
$user_input = '<script>alert(1);</script>';
$safe_output = htmlspecialchars($user_input, [1], [2], [3]);
echo $safe_output;
?>
Drag options to blanks, or click blank then click option'
AENT_QUOTES
BUTF-8
Ctrue
DENT_NOQUOTES
Attempts:
3 left
💡 Hint
Common Mistakes
Not escaping output leads to XSS vulnerabilities.
Using wrong flags or encoding causes improper escaping.