Jump to content

[SOLVED] is this function safe from sql injections?


scarhand

Recommended Posts

Hi I am currently in the process of learning about mysql_real_escape_string, stripslashes, addslashes, and other measures in order to protect my scripts from sql injections.

 

I was doing a tutorial and was wondering about the vulnerability (still a bit confusing to me but I will pick it up soon) and I used and partially modified a function for use in a script I'm working on.

 

I would like to know if this is function is safe from sql injections:

 

function confirmUser($username, $password)
{
   global $db;
   /* Add slashes if necessary (for query) */
   if(!get_magic_quotes_gpc()) 
   {
     $username = addslashes($username);
   }

   /* Verify that user is in database */
   $q = "select password from admin_users where username = '$username'";
   $result = mysql_query($q,$db);
   if(!$result || (mysql_numrows($result) < 1))
   {
     return 1; //Indicates username failure
   }

   /* Retrieve password from result, strip slashes */
   $dbarray = mysql_fetch_array($result);
   $dbarray['password'] = stripslashes($dbarray['password']);
   $password = stripslashes($password);

   /* Validate that password is correct */
   if($password == $dbarray['password'])
   {
     return 0; //Success! Username and password confirmed
   }
   else
   {
     return 2; //Indicates password failure
   }
}

 

Thank you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.