Currently when user logged in, i created 2 sessions.
$_SESSION['logged_in'] = 1;
$_SESSION['username'] = $username; // user's name
So that, those page which requires logged in, i just do this:
if(isset($_SESSION['logged_id'])){
// Do whatever I want
}
Is there any security loopholes? I mean, is it easy to hack my session? How does people hack session? and how do I prevent it??
EDIT:
Just found this:
http://www.xrvel.com/post/353/programming/make-a-secure-session-login-script
http://net.tutsplus.com/tutorials/php/secure-your-forms-with-form-keys/
Just found the links, are those methods good enough?? Please give your opinions. I still have not get the best answer yet.
This is the usual log-in code, some improvements can be made to make it harder to break. First, you can do a checksum with the username and the time of the log in or alternatively, a predefined string (or a salt), and store it in the session, and compare it.
So when the user log in:
And before entering a sensitive area:
By default, sessions are often store as files on the server side, and a cookie is put on the user's browser to remember which file to refer to. When it comes to session hacking, somehow the hacker retrieve enough information to duplicate the log-in or managed to change the session data, by using information from the cookie.
You could code your own session handling using databases for added security. Some stricter CMS, like Joomla, also logs the IP. However, this cause problems for people using certain ISP
This is ridiculous.
Session hijacking occurs when (usually through a cross site scripting attack) someone intercepts your sessionId (which is a cookie automatically sent to the web server by a browser).
Someone has posted this for example:
Lets go through what is wrong with this
Avoid manipulation by whom? magical session faeries? Your session variables will not be modified unless your server is compromised. The hash is only really there to nicely condense your string into a 48 character string (user agents can get a bit long).
At least however we're now checking some client data instead of checking SESSION to SESSION data, they've checked the HTTP_USER_AGENT (which is a string identifying the browser), this will probably be more than enough to protect you but you have to realise if the person has already taken your sessionId in someway, chances are you've also sent a request to the bad guys server and given the bad guy your user agent, so a smart hacker would spoof your user agent and defeat this protection.
Which is were you get to the sad truth.
As soon as your session ID is compromised, you're gone. You can check the remote address of the request and make sure that stays the same in all requests ( as I did ) and that'll work perfectly for 99% of your client base. Then one day you'll get a call from a user who uses a network with load balanced proxy servers, requests will be coming out from here through a group of different IPs (sometimes even on the wrong network) and he'll be losing his session left right and centre.
You can steal sessions via javascript (XSS->crossside scripting attack).. You should always use a salted MD5 Hash to secure your session.
To avoid session hijacking, you should put the user agent
into the hash as well.
In your example:
Before using the session, make sure it uses the correct hash: