Possible Duplicate:
How to prevent SQL injection in PHP?
I wanted to ask a few questions about protecting against sql injection. From what I've been reading I keep coming across these three things:
- stripslashes
- which is used in conjunction with magic_quotes_gpc
- mysql_real_escape_string (or mysqli I suppose in the newer php?)
The question is, should I be using both of these or will real_escape_string suffice?
For instance I have this line of code pertaining to a registration page (which I know for a fact is vulnerable as sqli helper let me find everything about my database :( as I've not implemented any of the above yet):
    if(isset($_POST['submit'])){
    //cleanup the variables
    $username = ($_POST['username']);
    $password = ($_POST['password']);
    $email = ($_POST['email']);
    $username = sanitise($username);
    $password = sanitise($password);
    $email = sanitise($email);
    //quick/simple validation
    if(empty($username)){ $action['result'] = 'error'; array_push($text,'Please type in a username'); }
    if(empty($password)){ $action['result'] = 'error'; array_push($text,'Please type in a password'); }
if($action['result'] != 'error'){
        $password = md5($password); 
        //add to the database
        $add = mysql_query("INSERT INTO Users VALUES(NULL,'$username','$password','$email',0, 'First', 'Last', 'Phone Number Here', '...', 'Something about me...')");
        if($add){
            //get the new user id
            $userid = mysql_insert_id();    
            //create a random key
            $key = $username . $email . date('mY');
            $key = md5($key);
            //add confirm row
            $confirm = mysql_query("INSERT INTO Confirm VALUES(NULL,'$userid','$key','$email')");   
            if($confirm){
                //include the swift class
                include_once 'swift/swift_required.php';
                //put info into an array to send to the function
                $info = array(
                    'username' => $username,
                    'email' => $email,
                    'key' => $key);
                //send the email
                if(send_email($info)){
                    //email sent
                    $action['result'] = 'success';
                    array_push($text,'Thanks for signing up. Please check your e-mail for confirmation.');
                }else{
                    $action['result'] = 'error';
                    array_push($text,'Could not send confirmation e-mail');
                }
            }else{
                $action['result'] = 'error';
                array_push($text,'Confirm row was not added to the database. Reason: ' . mysql_error());
            }
        }else{
            $action['result'] = 'error';
            array_push($text,'User could not be added to the database. Reason: ' . mysql_error());
        }
    }
    $action['text'] = $text;
}
?>
I thought my sanitisation function would help things - got it online, however it would appear it is a bit useless. Or perhaps it only helps against cross site scripting. Here it is:
 function cleanInput($input) {
        $search = array(
            '@<script[^>]*?>
.*?</script>@si',   // Strip out javascript
    '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
    '@<style[^>]*?>.*?
    </style>
@siU',    // Strip style tags properly
    '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
    );
    $output = preg_replace($search, '', $input);
    return $output;
    }
function sanitise($input) {
    if (is_array($input)) {
    foreach($input as $var=>$val) {
    $output[$var] = sanitise($val);
    }
    }
    else {
    if (get_magic_quotes_gpc()) {
    $input = stripslashes($input);
    }
    $input  = cleanInput($input);
    $output = $input;
    }
    return $output;
}
Would you suggest that function is useless?
If so, how would I go about securing the original bit of code? Namely:
    $username = ($_POST['username']);
    $password = ($_POST['password']);
    $email = ($_POST['email']);
 
     
     
     
    