Welcome to WebmasterWorld Guest from 54.167.82.170

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

SQL injections how to write against them

sql injections

     

pinto172

9:13 pm on Jul 23, 2007 (gmt 0)

5+ Year Member



I want to know how to prevent or try to resist sql injections in my code. I filter our everything except text in most of my forms but most use htmlspecialchars. Should I not use that?

Should I limit the amount of data input?

I mean what is everyone else doing that works?

eelixduppy

9:21 pm on Jul 23, 2007 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



The number one thing you really have to do is to escape your query variables. To do this, you can use mysql_real_escape_string [php.net] or mysql_escape_string [php.net].

jezzer300

9:25 pm on Jul 23, 2007 (gmt 0)

10+ Year Member



Any input field should have the addslashes() function put around the variable when building your sql command.

for example:

$reference = $_REQUEST['reference']

... use $reference in you code as usual...

$sql= "SELECT count(*) FROM track_page WHERE session_index='$session_index' AND reference='".addslashes($reference)."'";
$result = @mysql_query($sql);

Likewise, when you retrieve that data to display back to the user you may want to add htmlspecialchars($reference) around the code.

You can test your input fields by entering '

* I really would not reccomend using magic_quotes.

eelixduppy

9:32 pm on Jul 23, 2007 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



Just a quick note: addslashes shouldn't be used unless you cannot use the functions I linked to above. Not only are additional characters escaped with those functions but in the case of mysql_real_escape_string, the charset of the database is used, as well.

WesleyC

9:54 pm on Jul 23, 2007 (gmt 0)

5+ Year Member



SQL injection depends a lot on what version of SQL you're using--mysql_real_escape_string is definitely the best choice if you're using MySql, but for Microsoft SQL Server you can use...

str_replace( "'", "''", $evilInput );

Microsoft SQL interprets two single quotes ('') as a single escaped quote.

[edited by: WesleyC at 9:55 pm (utc) on July 23, 2007]

pinto172

4:06 am on Jul 24, 2007 (gmt 0)

5+ Year Member



I use only php and mysql on my applications.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month