Skip to content

Instantly share code, notes, and snippets.

@macek
Created April 15, 2011 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macek/921176 to your computer and use it in GitHub Desktop.
Save macek/921176 to your computer and use it in GitHub Desktop.
POS410 SQL sanitizer example
<?php
class Mysql {
static public function query(){
$args = func_get_args();
return mysql_query(call_user_func_array(array("self", "queryf"), $args));
}
static public function queryf(){
$args = func_get_args();
$query = array_shift($args);
return vsprintf($query, array_map("mysql_real_escape_string", $args));
}
}
# example usage: query user login
$email = "Robert O'Malley";
$password = "helloWorld11";
echo Mysql::queryf("SELECT * FROM users WHERE email='%s' AND password='%s';", $email, $password);
// => SELECT * FROM users WHERE email='Robert O\'Malley' AND password='helloWorld11';
# exapmle usage: sql injection
$email = "'; DROP TABLE users; -- ";
$password = "";
echo Mysql::queryf("SELECT * FROM users WHERE email='%s' AND password='%s';", $email, $password);
// => SELECT * FROM users WHERE email='\'; DROP TABLE users; -- ' AND password='';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment