Preventing SQL Injection with PHP
When you start working with dynamic websites, and therefore the use of data retreaved from a database, you always want to make sure your script is safe. $_POST and $_GET variables are simply to adjust in scripts and url's. Prevent SQL Injection by using a script like thisone. It checks whether the magic_quotes_gpc is enabled or not. If it is, it won't have to add the escaping slashes in front of each qoute or double quote. You don't want 2 slashes in front of it when it is enabled.
function:
[COLOR=#000000][COLOR=#0000BB]?php
[/COLOR][COLOR=#007700]function [/COLOR][COLOR=#0000BB]mysql_prepare[/COLOR][COLOR=#007700]( [/COLOR][COLOR=#0000BB]$value [/COLOR][COLOR=#007700]) {
[/COLOR][COLOR=#0000BB]$magic_quotes_active [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]get_magic_quotes_gpc[/COLOR][COLOR=#007700]();
[/COLOR][COLOR=#0000BB]$new_enough_php [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]function_exists[/COLOR][COLOR=#007700]( [/COLOR][COLOR=#DD0000]"mysql_real_escape_string" [/COLOR][COLOR=#007700]);
if( [/COLOR][COLOR=#0000BB]$new_enough_php [/COLOR][COLOR=#007700]) { [/COLOR][COLOR=#FF8000]// PHP v4.3.0 or higher
[/COLOR][COLOR=#007700]if( [/COLOR][COLOR=#0000BB]$magic_quotes_active [/COLOR][COLOR=#007700]) { [/COLOR][COLOR=#0000BB]$value [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]stripslashes[/COLOR][COLOR=#007700]( [/COLOR][COLOR=#0000BB]$value [/COLOR][COLOR=#007700]); }
[/COLOR][COLOR=#0000BB]$value [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]mysql_real_escape_string[/COLOR][COLOR=#007700]( [/COLOR][COLOR=#0000BB]$value [/COLOR][COLOR=#007700]);
} else { [/COLOR][COLOR=#FF8000]// before PHP v4.3.0
[/COLOR][COLOR=#007700]if( ![/COLOR][COLOR=#0000BB]$magic_quotes_active [/COLOR][COLOR=#007700]) { [/COLOR][COLOR=#0000BB]$value [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]addslashes[/COLOR][COLOR=#007700]( [/COLOR][COLOR=#0000BB]$value [/COLOR][COLOR=#007700]); }
}
return [/COLOR][COLOR=#0000BB]$value[/COLOR][COLOR=#007700];
}
[/COLOR][COLOR=#0000BB]?>[/COLOR][/COLOR]
example:
[COLOR=#000000][COLOR=#0000BB]<?php
$id [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]mysql_prepare[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$_GET[/COLOR][COLOR=#007700][[/COLOR][COLOR=#DD0000]'id'[/COLOR][COLOR=#007700]]);
[/COLOR][COLOR=#0000BB]$query [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#DD0000]"SELECT * FROM pages WHERE id = "[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]$id[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB]mysql_query[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$query[/COLOR][COLOR=#007700]);
[/COLOR][COLOR=#0000BB]?>[/COLOR][/COLOR]
use this idea, works fine.