Skip to content

Instantly share code, notes, and snippets.

@nijjwal
Created March 31, 2015 03:20
Show Gist options
  • Save nijjwal/4f1cb243036a7f31db55 to your computer and use it in GitHub Desktop.
Save nijjwal/4f1cb243036a7f31db55 to your computer and use it in GitHub Desktop.
sql-injection-prevention
<?php
require 'pdo.php';
//1. Create an instance of connection
$pdo_obj = new Connection();
//2. Connect to the server + db
try
{
$connection = $pdo_obj->connect();
}catch(Exception $e){
echo $e->getMessage();
}
//3. Imagine that you are getting input from user
//We will use this string for injecting vulnerable code
$id = "anna' OR '1'='1";
//4. Prepare sql query
$sql = "SELECT * FROM USER WHERE id = ?";
echo $sql;
//5. Prepare, execute, and display
$stmt = $connection->prepare($sql);
$stmt->execute(array($id));
//$stmt->bindParam(':ids',$id,PDO::PARAM_INT);
//$stmt->execute();
$arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC);
//If the db returns some result
if($arrValues)
{
echo "<table border='1px'>\n";
echo "<tr>\n";
foreach ($arrValues[0] as $key => $useless){
echo "<td>$key</td>";
}
echo "</tr>\n";
//display data
foreach($arrValues as $rows)
{
echo "<tr>";
foreach($rows as $key=>$value)
{
echo "<td>".$value."</td>";
}
echo "</tr>";
}
echo "</table>";
}
else
{
echo "<br/>No results were found!";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment