PHP Session Hijacking

Session Hijacking With PHP

Session hijacking is a term that is used to describe a method for obtaining a user's PHPSESSID. When a user logs into a PHP application, the browser will store a hash string value like'525cc0036c99f013bd17b7b91233fae4'. The same hash string matches the stored session on the server. There are several ways the user can get your sessions; such as sniffing it out on a shared network with software like 'Wireshark'. Another method is to just get the id from a public computer and manually recreate it in another browser. The whole idea here is that if the browser PHPSESSID and the server session id will match, any user can make the website believe that you are authenticated.

As a programmer, there are several safeguards you could use to your scripts to ensure that the PHPSESSID in your browser is not so usable elswhere. You can set a session variable as your ipaddress with $_SERVER[‘REMOTE_ADDR’]. For example, $_SESSION[ip_address'] = $_SERVER[‘REMOTE_ADDR’]. Now, you can run the following code to make sure the session ip is the same as the computer ip address.

 if($_SERVER[‘REMOTE_ADDR’] != $_SESSION[ip_address'] ){ die(); } 

Regenerate Session ID

Another method to deter a session hijack is to use the function session_regenerate_id();. This function gives the user a new session id and makes the old session id unuseful. This function could be used in any page or script where you want to make that change.


SSL can be implemented to a website, or cpanel account to prevent session hijacking. Adding an SSL certificate can be as cheap as $35 / year.