PHP SQL Injection
SQL Injection in PHP is a security vulnerability that allows an attacker to manipulate SQL queries by injecting malicious input into a form field or URL parameter. If your PHP code directly inserts user input into SQL statements without validation or escaping, attackers can bypass login, steal data, or even delete your database.
SQL Injection एक सुरक्षा समस्या है जिसमें हमलावर आपकी वेबसाइट के डेटाबेस क्वेरी में छेड़छाड़ कर सकता है।
Unsafe SQL Query (Vulnerable to Injection)
<?php
$email = $_GET['email'];
$sql = "SELECT * FROM users WHERE email = '$email'";
$result = mysqli_query($conn, $sql);
?>
This code is unsafe because it directly inserts user input into the SQL query, which makes it vulnerable to SQL Injection attacks. If a user visits your page like this:
https://example.com/page.php?email=' OR '1'='1'
This condition is always true ('1'='1'), so all user records will be returned, even without a valid email.
Why it's dangerous:
- Attackers can bypass login
- Access, modify or delete data
- Drop entire tables (e.g. DROP TABLE users)
- Expose sensitive user information
यह कोड असुरक्षित है क्योंकि यह उपयोगकर्ता के इनपुट को सीधे SQL क्वेरी में डालता है, जिससे SQL Injection का खतरा बढ़ जाता है। उदाहरण के लिए:
https://example.com/page.php?email=' OR '1'='1'
यह कंडीशन हमेशा सही होती है ('1'='1'), इसलिए सभी यूजर रिकॉर्ड दिखाए जा सकते हैं।
यह खतरनाक क्यों है:
- हमलावर लॉगिन को बायपास कर सकता है
- डेटा को एक्सेस, संशोधित या डिलीट कर सकता है
- पूरा टेबल हटा सकता है (जैसे DROP TABLE users)
- संवेदनशील जानकारी उजागर कर सकता है
Tips to Stay Safe
- Always use prepared statements (MySQLi or PDO)
- Never directly insert user input into SQL queries
- Validate and sanitize input
- Store passwords using
password_hash()
andpassword_verify()
- हमेशा prepared statements (MySQLi या PDO) का उपयोग करें
- यूज़र इनपुट को सीधे SQL क्वेरी में न डालें
- इनपुट को validate और sanitize करें
- पासवर्ड को
password_hash()
औरpassword_verify()
से सुरक्षित रखें
Safe with MySQLi Prepared Statement
This code is safe from SQL Injection because it uses prepared statements with parameter binding — a secure way to handle user input.
- Query is prepared with placeholders (? marks)
- bind_param safely inserts user values
- SQL engine escapes dangerous input automatically
यह कोड SQL Injection से सुरक्षित है क्योंकि यह prepared statements और parameter binding का उपयोग करता है, जो कि इनपुट को सुरक्षित तरीके से संभालने का तरीका है।
- क्वेरी पहले से तैयार की जाती है (placeholders के साथ)
- bind_param यूजर वैल्यू को सुरक्षित रूप से जोड़ता है
- MySQL खतरनाक इनपुट को खुद ही escape करता है
<?php
$conn = new mysqli("localhost", "root", "", "test");
$stmt = $conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$email = $_GET['email'];
$stmt->execute();
$result = $stmt->get_result();
?>
Safe with PDO
PDO also protects against SQL Injection by using prepared statements and named parameters like :email
.
PDO भी SQL Injection से सुरक्षा प्रदान करता है, क्योंकि यह prepared statements और named parameters (जैसे :email
) का उपयोग करता है।
<?php
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "");
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email");
$stmt->execute(['email' => $_GET['email']]);
$result = $stmt->fetchAll();
?>