Jump to content

[SOLVED] MySQL Injection Prevention


xProteuSx

Recommended Posts

I am looking to prevent MySQL injection attacks on my site.  I was wondering if this is the correct way of doing things.  I cannot get this to work ...

 

Here is the original code:

 

--------------------------------------------------------------------------------------------------------------------------

$loginhandle = $_POST['username'];

$loginpassword = $_POST['password'];

include ("include/db.php");

$query = "SELECT * FROM users WHERE users_handle = '$loginhandle' AND users_password = '$loginpassword'";

$userstatsresult = mysql_query($query) or die ('Error in query 0: ' . mysql_error());

 

--------------------------------------------------------------------------------------------------------------------------

 

My understanding is that I have to do the following:

 

--------------------------------------------------------------------------------------------------------------------------

$loginhandle = mysql_real_escape_string($_POST['username']);

$loginpassword = mysql_real_escape_string($_POST['password']);

include ("include/db.php");

$query = "SELECT * FROM users WHERE users_handle = '$loginhandle' AND users_password = '$loginpassword'";

$userstatsresult = mysql_query($query) or die ('Error in query 0: ' . mysql_error());

 

--------------------------------------------------------------------------------------------------------------------------

 

However, this does not work.  The original code executes correctly, and I can log in.  However, after I add the mysql_real_escape_string() functions I get a series of errors:

 

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'username@'server' (using password: NO) in /loginconf.php on line 25

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in loginconf.php on line 25

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'username@'server' (using password: NO) in /loginconf.php on line 26

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /loginconf.php on line 26

 

Any reason why this would bugger up the connection code?

Link to comment
Share on other sites

So he is right in saying that..

 

mysql_real_escape_string($_POST['whatever']);

 

WILL prevent a mysql injection?

 

Because at the moment thats what im using, just wanna know if there is anything better, or do anything differently to prevent this?

 

Thanks

Link to comment
Share on other sites

Booya - Kasha!  Thanks 'cooldude832'!  Just had to change the order of things.  Solved it by doing this;

 

$loginhandle = $_POST['username'];

$loginpassword = $_POST['password'];

include ("include/db.php");

$loginhandle = mysql_real_escape_string($loginhandle);

$loginpassword = mysql_real_escape_string($loginpassword);

 

Just had to do the mysql_real_escape_string() nonsense following the inclusion of db.php.

Link to comment
Share on other sites

It will surely help prevent it.

 

So he is right in saying that..

 

mysql_real_escape_string($_POST['whatever']);

 

WILL prevent a mysql injection?

 

Because at the moment thats what im using, just wanna know if there is anything better, or do anything differently to prevent this?

 

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.