
Originally Posted by
kuau
To me this implies that by having all the private files in one folder, I can protect the whole folder. I don't know how to do that except with htaccess (?). Maybe I am not understanding what you mean.
what Daniel said.

Originally Posted by
kuau
Did you find the code you use to clean user input? I would really appreciate having something an expert considers good protection against SQL injection because I'm not sure what the bad guys can do so am not sure how to protect against it. Thanks.
[...]
As for magic quotes, if I turn it off in php.ini, I know it then causes problems if there are apostrophes in the text. Is addslashes($str) sufficient to do what magic quotes would have done, and if so, isn't that just as insecure as magic quotes?

Originally Posted by
djr33
For MySQL injection, the single function mysql_real_escape_string() does everything you need.
[...]
On the other hand, if you want to write code that can be portable (installed easily on any server) then checking for that, even if it is off on your server, wouldn't hurt. But that's not important unless you're distributing your scripts to others (or using them for multiple clients and/or on multiple personal servers).
[...]
Magic_quotes is bad. You don't want to replace its behavior if you turn it off-- you want to get rid of it completely. You don't need to add slashes or do anything. Now, in some cases, magic_quotes is useful, but not in all cases. The problem is that it is inconsistent. So, if you turn it off then do everything yourself, it will be consistent and you never need to
That is the situation (portability) that I was referring to; but it beneficial even if you don't even plan to share your code: do it once and forget about it.
to recap:
1. You want to completely get rid of magic_quotes_gpc.
2. You can't effectively do that from inside your php script - only via server configuration.
3. You might not have access to your php.ini, and/or you might forget about it at some point.
This takes care of that for you: counteracts the affects of magic_quotes_gpc if it's turned on; does absolutely nothing if it's turned off.
PHP Code:
// if get_magic_quotes_gpc is set in your config, kill it! KILL IT!
// (using ini_set('magic_quotes_runtime', 0); is useless:
// the evil slashing has already happened,
// and the setting is restored to its default next time a script runs.)
if(get_magic_quotes_gpc()){
// make strip_slashes() recursive, so it affects _all_ values in the GPC arrays
function stripslashes_recursive( $v ){
return is_array( $v )?
array_map( 'stripslashes_recursive',$v ):
stripslashes( $v );
}
// if you didn't know, GPC stands for GetPostCookie,
// so those are the superglobals you need to "fix":
$_GET = stripslashes_recursive( $_GET );
$_POST = stripslashes_recursive( $_POST );
$_COOKIE = stripslashes_recursive( $_COOKIE );
}
// I also do this
// ( $_REQUEST is unrelated to our magic_quotes problem, but evil nonetheless )
$_REQUEST = NULL;
this solves your Magic Quotes problem, which un-complicates (does not solve) your SQL injection issues.
You must still use mysql_real_escape_string()
(or switch to mysqli or PDO and use prepared statements
) to sanitize your user inputs - this is not intended to replace the need for that.
Edit:
As for your question about "using $_GET" with your "forgot password" script:
when a user says they forgot their password, you might do something like this:
1. create a temporary password (something random and hard-to-guess)
2. store the temp password in a special table in your DB, along with the username and email it belongs to, and the time it was created
3. send the temp password to the user's email - BUT, instead of saying "here's your temp password," give them a link like this:
PHP Code:
'<a href="http://example.com/lostpass.php?email='.$user_email.'&passonce='.$temp_password.'">click here to recover your password</a>
4. when they click on the link, you can check the user email against the temp password (and how long ago the password was created; it's good to make these expire within a day at most).
This way, recovering their password is as convenient as possible: just click the link and follow directions.
You might also give them the temp_password, so they can enter it manually, in case they have an objection to clicking on links in emails (if they just requested a new password, they ought to trust you; but it is a good security habit).
IF the temp password is valid:
1. log the user in (only for the session).
2. redirect them to the "change your password" page and tell them to change their password (good idea to make this _required_).
3. delete the temp password so it can not be used again.
REALISTICALLY, even though the reset-pass-via-email approach is widely used (even by google and amazon, among others), the concept is not fool-proof. It relies on two things that you can't be sure of:
1. the user's _current_ email address is in your records.
2. the user's email account has not been compromised.
However, this is all beyond your control, and responsibility for both issues rests squarely with the user.
Bookmarks