Is Presentation Sqli
Is Presentation Sqli
SQL INJECTION
Vulnerable Code
• <?php
• // Database connection
• $conn = mysqli_connect("localhost", "root", "your_password", "example_db"); Vulnerability
• if (!$conn) {
If the attacker provides the
• die("Connection failed: " . mysqli_connect_error()); following input:
• }
username: admin
• // User input (from form or query parameters) password: ' OR '1'='1
• $username = $_GET['username'];
• $password = $_GET['password'];
• if (mysqli_num_rows($result) > 0) {
• echo "Login successful!";
• } else {
• echo "Invalid credentials.";
• }
• mysqli_close($conn);
• ?>
• <?php
Secure Code
• // Database connection
• $conn = mysqli_connect("localhost", "root", "your_password", "example_db");
• if (!$conn) {
• die("Connection failed: " . mysqli_connect_error());
• }
• if (mysqli_num_rows($result) > 0) {
• echo "Login successful!";
• } else {
• echo "Invalid credentials.";
• }
• mysqli_stmt_close($stmt);
• mysqli_close($conn);
• ?>
If the attacker provides the following input:
•username: admin
•password: ' OR '1'='1
Secure Code
• Why It’s Secure
• Bound Parameters: The ? placeholders ensure that user inputs are treated as data, not executable
SQL code.
• Automatic Escaping: Special characters in user inputs are automatically escaped.
• Prevention of Query Manipulation: User inputs cannot alter the structure of the SQL query.
Any Question?
Thank You