This question is everywhere, and I looked at many answers. A lot suggest using mysql_real_escape_string
but that isn't an option, since it's depreciated now.
Being a rookie at PHP/SQL and especially when it comes to security, I can only hope some of you may be able to help out here. I know for a fact that some of these procedures I already use contribute to security.
As an example: I didn't use :pname
before when defining query_params
, which caused SQL to interpret submissions like 'values'; drop table dbname;
as actual commands. One thing that did save me from the command not being executed is the conversion of spaces to underscores.
search_submit.php
<?php
$pname=str_replace(' ','_',strtolower($_GET['pname']));
require 'connection.php';
if(isset($_GET['pname'])){
if(preg_match("/\w/i", $_GET['pname'])){
$query="SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = :pname;";
$query_params = array(
':pname' => str_replace(' ','_',strtolower($_GET['pname']))
);
$prep=$conn->prepare($query);
$result=$prep->execute($query_params) or die("failed!");
$row = $prep->fetch();
if($row)
{
die("Exists");
}
else{
echo("doesn't");
}
}
}
else{
echo "<p>Error</p>";
}
?>
index.html
<form method="get" action="search_submit.php" id="searchform">
<input type="text" name="pname">
<input type="submit" name="input" value="Search">
As of right now everything appears to be working perfectly, and any sneaky submissions are simply interpreted as text. But I am a very paranoid person.