Computing.Net > Forums > Web Development > Preventing MYSQL Injection Attacks!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Preventing MYSQL Injection Attacks!

Reply to Message Icon

Name: tubs
Date: January 15, 2009 at 09:55:46 Pacific
OS: Microsoft Windows Pista
CPU/Ram: 2.394 GHz / 3070 MB
Product: Hewlett-packard / Gj311aa-abu m8180.uk
Subcategory: PHP
Comment:

Hey,

Im currently working on a small script for my website it is powered by a MYSQL DB and I have come in to one big problem…

I have created a function to stop those pesky MYSQL injection’s but it likes to remove all ‘ from the text that I input.

Only problem is that I can’t use it as punctuation how could I stop it form replacing ‘ with \’

So sentences like

Joe’s blog’s are amazing!!!

Don’t become

Joe\’s blog\’s are amazing!!!

All help is very much appreciated!



Sponsored Link
Ads by Google

Response Number 1
Name: tubs
Date: January 15, 2009 at 13:34:04 Pacific
Reply:

oh and i forgot to add that im useing mysql_real_escape_string and strip_tags


0

Response Number 2
Name: Fist (by fmwap)
Date: January 16, 2009 at 20:19:51 Pacific
Reply:

use stripslashes
after reading the data out of the database.

Technically though, you should be using a prepared statement & then you don't need to worry about quoting.


0

Response Number 3
Name: Michael J (by mjdamato)
Date: January 17, 2009 at 01:54:13 Pacific
Reply:

I'm guessing your PHP configuraion has Magic Quotes turned on: http://www.php.net/magic_quotes
When Magic Quotes is turned on any data coming from a form input will automatically have slashes added to the values. Then when mysql_real_escape_string() is used it adds additional slashes on top of that!

User Input: Bob's Store
POST Data: Bob\'s Store
mysql_real_escape_string() output:
Bob\\\'s Store

As Fist states you would want to run strip_slashes on the POST data and then run mysql_real_escape_string() on it. However, if you want your code to be portable - move to any server - you need to code such that it will work whether magic quotes are on or off.

Instead of using mysql_real_escape_string() directly, you can create a custom function that will work no matter the environment.

function dbSafe($string)
{
  if (get_magic_quotes_gpc())
  {
    $string = stripslashes($string);
  }
  return mysql_real_escape_string($string);
}

Michael J


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Preventing MYSQL Injection Attacks!

Dam PHP! www.computing.net/answers/webdevel/dam-php/752.html

PHP: Format Data for DB Insertion www.computing.net/answers/webdevel/php-format-data-for-db-insertion/2744.html

need to create onclick scroller www.computing.net/answers/webdevel/need-to-create-onclick-scroller/3600.html