Jump to content

Salt Generation - Issue with Quotes


Mko

Recommended Posts

I have this little snippet of code that runs when a user updates their password:

 

fetch_user_salt_new():

function fetch_user_salt_new($length = 5)
{
$salt_a = '';
for ($i = 0; $i < $length; $i++)
{
	$salt_a .= chr(vbrand(33, 126));
}
return $salt_a;
}

 

$salt = fetch_user_salt_new();
$salt_processed = mysql_real_escape_string($salt);

 

Now, occasionally when a user changes their password (or anything that inserts the salt into the database, such as registration), the salt length stored in the database becomes 6 or 7 instead of 5.

As in, 99% of salts are only 5 digits long, but some salts are longer...

The longer salts normally have odd components, such as \', \", or \\  leading to salts increasing by 1 or 2 digits in length.

 

My idea is that mysql_real_escape_string() is putting a \ in front of quotes which is not what I intended when adding that piece of code in. By adding mysql_real_escape_string() in, I intended for quotes (' or ") to not be factors affecting the Query. Prior to instituting mysql_real_escape_string(), a ' or " would close the query and mess up the insertion of the salt. (Original Topic: http://www.phpfreaks.com/forums/index.php?topic=356368.0 )  It seemed to work but not doesn't :(

 

 

Any help is very appreciated,

Mark

Link to comment
Share on other sites

If your slashes are getting stored in the value, then your doing something wrong which is causing the value to get escaped twice.  Either your running mysql_real_escape_string on the value twice, or maybe using mysql_real_escape_string on the value then inserting it using a prepared statement and binding the value.

 

 

Link to comment
Share on other sites

If your slashes are getting stored in the value, then your doing something wrong which is causing the value to get escaped twice.  Either your running mysql_real_escape_string on the value twice, or maybe using mysql_real_escape_string on the value then inserting it using a prepared statement and binding the value.

Yeah I figured out that was it, thanks :)

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.