Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (1)

Powered by Vanilla. Made with Bootstrap.
Anti SQL Injection
  • undead
    Posts: 822
    Let's say we have a query like this:

    $query = "SELECT * FROM test_table WHERE id=$id";

    This is vulnerable to SQL Injection because the variable $id has not protection.
    To avoid this we can use a PHP function, intval.
    To see what it does go here:
    http://www.php.net/intval

    So if we use it:

    $safe = intval($id);
    $query = "SELECT * FROM test_table WHERE id=$safe";

    Another way is to make our query like this:
    $query = "SELECT * FROM test_table WHERE id='".$id."'";

    Another way also is to use is_numeric():

    if (is_numeric ($_GET['id']) ) {
    $query = "SELECT * FROM test_table WHERE id={$_GET['id']}";
    } else { echo"Again nooby"; exit; }