For the past week I've been reading up on security concerning php.
After reading paper after paper, tutorial after tutorial, I feel more lost than ever.
I can't seem to get my head around securing my php code!
So here I am, hopefully you guys can help me out
(I have searched the forum, but I still do not have a clear understanding)
1) Securing User Input
addslashes, stripslashes, strip_tags, htmlspecialchars, trim...
I understand all these functions (thanks to php.net) although I'm not sure how exactly I should use them (thanks to magic quotes). Do I do something like this?:
a. check if magic quotes are on, if not then addslashes to post, get, cookies
b. Then before data is printed to the screen, remove the slashes
(kinda what phpbb2 does)
Or do I do this:
a. check if magic quotes are on, if IT IS then stripslashes from post, get cookies
b. addslashes to every user inputed variable in a QUERY
(kinda what vbulletin does)
How about htmlspecialchars? When should I use them?
Couldn't I just use a database abstraction layer, and then addslashes(htmlspecialchars($var) in the mysql_query (so that i only need to do it once)?
I feel so lost
2) Preventing XSS Attacks
How? I created an XSS Clean Function, but I don't know where to use it
function clean_xss($input)
{
$input = preg_replace( '/javascript/i', 'java script', $input );
$input = str_replace( '"', '"', $input );
$input = str_replace( '(', '(', $input );
$input = str_replace( ')', ')', $input );
$input = str_replace( '#', '#', $input );
$input = str_replace( '&', '&', $input );
$input = str_replace( '<', '<', $input );
$input = str_replace( '>', '>', $input );
return $input;
}
3) $GLOBALS
Okay last question, in many scripts I see the variable '$GLOBALS'.
Is that a predefined variable containing all the global variables?
So could I do something like this:
if(get_magic_quotes_gpc())
{
StripSlashesArray($GLOBALS);
}