content © Sydney PHP Group • 2006
Sydney PHP Group - A session database handler class - 2 of 2
It's recommended to point a browser at http://php.net/sessions during this presentation
A short summary
A quick example
Methods for securing the session
$_SESSION array.$_GET or via form posts using $_POST)Any system that PHP can communicate with:
Drop in to your code for instant session handling
Use the session library from Part 1 to provide a simple login logout scenario
As it stands, the session handler has one major weakness - it relies on the session id provided without any other form of authentication.
Someone can grab a session id and pretend to be another user on the site.
The session handler would happily serve the false user someone else's information.
Looking beyond just the session id....
Sessions can be hijacked by third parties. This can be done via:
Record the initial requestors IP address and only allow that IP to, at a minimum, read session data
Note: multiple users can come from the same IP address.
This method won't catch these users.
Once a user has logged in successfully, give them a new session id and destroy the original session id.
This will help to avoid session fixation
Use session_regenerate_id() - a PHP session function
Session ids stored in the URL - like http://example.com?sid=4567 can be stored in web logs, browser caches, search engine results.
If the session 4567 remains in the database then anyone can access that data.
One possibility is to give the user a second cookie. This cookie is only available to the initial user who logged in and they must provide it when accessing secure pages, along with their session id.
The second session id should be stored in a separate data store to the main session id.
In a session fixation attack, the 3rd party would only have the initial session id and not the session id from the second cookie provided
Don't allow old session data to hang around.
Make sure your session destroy function really does destroy the data.
If you want to store historical session data for some purposes, for instance user tracking, aggregate statistics, trend data then migrate this data to another database.
Finally, think about what data you are storing in the session. If someone does compromise a session what data are you giving away?
Parts 1 and 2 will be available online at http://sydphp.org/presentations