virtuexru Posted May 29, 2007 Share Posted May 29, 2007 As far as checking to see if users are logged in, are cookies secure enough? Can a hacker edit a cookie somehow? This is what I use right now: if($_COOKIE['user_logged'] == false) { echo "sensitive information"; } else { include('login.php'); } Quote Link to comment https://forums.phpfreaks.com/topic/53469-php-security/ Share on other sites More sharing options...
per1os Posted May 29, 2007 Share Posted May 29, 2007 Usually you store the username or userid with a md5 hashed password to verify against the db on each page. This can be done in cookies or in a session variable. As long as the password is hashed up so it is not visible it is a pretty secure way, there are always flaws that can happen but yes that is how most sites work. Quote Link to comment https://forums.phpfreaks.com/topic/53469-php-security/#findComment-264269 Share on other sites More sharing options...
virtuexru Posted May 29, 2007 Author Share Posted May 29, 2007 Well, I basically did this: If you are not authed ($_COOKIE['user_logged'] == false), I redirect you to login. There, I hash the PW in SHA1, set it against the DB, if everything is OK you get a cookie that you have been logged successfully (which expires in 12 hours). Once that's done, all the other pages I just check against whether or not user cookie has been logged. Quote Link to comment https://forums.phpfreaks.com/topic/53469-php-security/#findComment-264273 Share on other sites More sharing options...
MadTechie Posted May 29, 2007 Share Posted May 29, 2007 bad idea to store login settings in a cookie, store their name sure but they could edit the cookie in anyway they see fit ie without loggin in they could chage the cookie to true and their in.. unless you have other checks Quote Link to comment https://forums.phpfreaks.com/topic/53469-php-security/#findComment-264276 Share on other sites More sharing options...
virtuexru Posted May 29, 2007 Author Share Posted May 29, 2007 What if I made the cookie $_COOKIE['user_logged'] to equal some kind of random 16-bit hash? Quote Link to comment https://forums.phpfreaks.com/topic/53469-php-security/#findComment-264278 Share on other sites More sharing options...
MadTechie Posted May 29, 2007 Share Posted May 29, 2007 OK look at it this way you assume it will expire in 12 hours but they can change that to 12 years.. cookies are not secure.. anything that a user can edit is basically a security risk.. to make it harder i guess you could have 2 cookies 1. $_COOKIE['user_logged'] = date&time (12 hour from set) 2. $_COOKIE['user_loggedH'] = MD5+SALT of $_COOKIE['user_logged'] the salt could be static to your site (dynamic is better) so you check salt+md5 = $_COOKIE['user_logged'] then check if that time has expired.. just an idea Quote Link to comment https://forums.phpfreaks.com/topic/53469-php-security/#findComment-264285 Share on other sites More sharing options...
conker87 Posted May 30, 2007 Share Posted May 30, 2007 I store the username and password in a cookie. But before every "sensitive" page/data is displayed I have the following code: <?php if (isset($_COOKIE['my_username_cookie'])) { $usernameC = $_COOKIE['my_username_cookie']; $passwordC = $_COOKIE['my_password_cookie']; $check = mysql_query("SELECT * FROM `members_table` WHERE `username_field` = '$usernameC'")or die(mysql_error()); while($infoCheck = mysql_fetch_array($check)) { if ($passwordC != $infoCheck['password']) { // If the password cookie has been changed, this will happen. } else { // All's well! Do the including of sensitive info here. } } ?> That's how I do it, if the password (which is md5(), obviously) in the cookie is not the same as the password in db then they get a lovely error. Thank you about.com! Quote Link to comment https://forums.phpfreaks.com/topic/53469-php-security/#findComment-264403 Share on other sites More sharing options...
MadTechie Posted May 30, 2007 Share Posted May 30, 2007 OK let me just logon to your site and get a cookie set.. Great.. now let me edit the username to include some SQL injection thats the first problem i see ie set the cookie to a' or 1=1-- so SQL = SELECT * FROM `members_table` WHERE `username_field` = 'a' or 1=1--' of course i could just drop your tables or update the password infact anything i please. in other words filter ALL user input this includes cookies as they can be edited by the user as for storing the password, i hope you also used salt.. personally i could have a "tagcode" or the UserID so if cookie_userid & cookie_username match and leave the password out.. (maybe md5 the username) Quote Link to comment https://forums.phpfreaks.com/topic/53469-php-security/#findComment-264566 Share on other sites More sharing options...
conker87 Posted May 30, 2007 Share Posted May 30, 2007 Would sessions be safer? Using the PHPSESSION=<loadsofcraphere> in the URL? Quote Link to comment https://forums.phpfreaks.com/topic/53469-php-security/#findComment-264923 Share on other sites More sharing options...
MadTechie Posted May 30, 2007 Share Posted May 30, 2007 Sessions are safer, but expire (based on server settings) you could applie a filter <?php $usernameC = preg_replace('/[^0-9a-z]/i', '', $_COOKIE['my_username_cookie']); $passwordC = preg_replace('/[^0-9a-z]/i', '', $_COOKIE['my_password_cookie']); ?> will change a' or 1=1-- to safer aor11 it will removel everything thats NOT a-z (upper and lower case) or 0-9. Quote Link to comment https://forums.phpfreaks.com/topic/53469-php-security/#findComment-265148 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.