title: Effective way to protect session hijacking in php
body: <p>I read on how to protect a session in php, there are a few, but they are not so effective like adding to a session the useragent,ip and port and encrypting them both.
  What is a good way to prevent session hijacking?
  I thought to take the following steps:</p>
  
  <ol>
  <li>Change the PHPSESSID key altogether to something generic like id </li>
  <li>Generate token per page and put it on the page and then validate it as well as teh session. That would reduce my reliance on session alone for validation.</li>
  <li>I would add a short session expiration. </li>
  <li>Add more variables to the session id and encrypt it, so it would be longer and harder to crack. Perhaps I would use RSA encryption.</li>
  <li>Put a logout  button, so that the user will be able to terminate his session.</li>
  <li>Use javascript to count the time, over 5 mins will alert the user to continue his          session. </li>
  <li>Save session in cookies only.</li>
  </ol>
  
  <p>The difficulties that I heard are: <em>When you use token per page, you need to disable the back button? why is that?</em></p>
  
  <p>A few other things are also unclear? Is saving session in the database more secure? why?
  How more secure is to use SSL? How about regenerating session id very quickly, would it help?</p>
  
  <p>What system would prevent brute-forcing the encryption key (would identifying the ip of the user who tries to flood the server with massive attempts to guess the session id would help?)?</p>
  
  <p>How does session regeneration works, is the old session key destroyed automatically, what if the hacker obtains an old session key would it still work? Please, it is important for me to understand session security, because I am learning how to be a penetration tester?</p>
  
  <p><strong>UPDATE</strong>
  I thought to do this:
  Symmetric encryption on the session id with key A
  Symmetric encryption on a randomly generated token that will be in the post field with key </p>
  
  <p>The randomly generated token will be appended to the session id too and then encrypted.</p>
  
  <p>On Request, I should get those variables:
  $_SESSION['PHPSESSID'] (has the randomly generated token encrypted in it)
  $_POST['RandomlyGeneratedToken'] </p>
  
  <p>Decrypt session id with key A and decrypt randomly token with key B.
  Do 2 checks:
  -Check the token if it is the same as the token on the first request sent.
  -Check if the token exists in the sessionid.</p>
  
  <p>Possibility for the hacker:
  -Bruteforce the session id.
  My session id is long enough it would take him time. I could employ a system that detects massive flow of requests from the same ip with different session id and slow him down with the sleep function.</p>
  
  <p>-Eavesdrop the traffic and get the session id from the user and also the token and try to submit those.
  hmm... I will have to regenerate the session id with every request and expire the session quickly on certain pages..may be 1 minute..
  But how fast can he eavesdrop?</p>