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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.