SQL injections
<?php $servername = "localhost"; $username = "root"; $password = "123"; $dbname = "testsqli"; function makeSafe($string, $is_gpc = true){ //if($is_gpc) $string = (get_magic_quotes_gpc() ? stripslashes($string) : $string); //if(!db_link()){ sql("select 1+1", $eo); } // prevent double escaping $na = explode(',', "\x00,\n,\r,',\",\x1a"); $escaped = true; $nosc = true; // no special chars exist $x = 1; foreach($na as $ns){ $dan = substr_count($string, $ns); $esdan = substr_count($string, "\\{$ns}"); echo $x . " " . $dan . " " . $esdan . "\n"; $x = $x + 1; if($dan != $esdan) $escaped = false; if($dan) $nosc = false; } if($nosc){ // find unescaped \ $dan = substr_count($string, '\\'); $esdan = substr_count($string, '\\\\'); if($dan != $esdan * 2) $escaped = false; } if ($escaped == true) {echo "true\n";} if ($escaped == false) {echo "false\n";} return $escaped; //return ($escaped ? $string : db_escape($string)); } // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } //$x = $_GET['p1']; $x = "\\\\'\\\\\\'or 1=1; -- "; if(makeSafe($x) == false){ exit("false"); } $sql = "SELECT id, first, last FROM person where first = '$x'; "; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "<br> id: ". $row["id"]. " - Name: ". $row["first"]. " " . $row["last"] . "<br>"; } } else { echo "0 results"; } $conn->close();