Note that there are some explanatory texts on larger screens.

plurals
  1. POProper session hijacking prevention in PHP
    primarykey
    data
    text
    <p>I know this topic has been discussed <em>a lot</em>, but I have a few specific questions still not answered. For example:</p> <pre><code>// **PREVENTING SESSION HIJACKING** // Prevents javascript XSS attacks aimed to steal the session ID ini_set('session.cookie_httponly', 1); // Adds entropy into the randomization of the session ID, as PHP's random number // generator has some known flaws ini_set('session.entropy_file', '/dev/urandom'); // Uses a strong hash ini_set('session.hash_function', 'whirlpool'); </code></pre> <hr> <pre><code>// **PREVENTING SESSION FIXATION** // Session ID cannot be passed through URLs ini_set('session.use_only_cookies', 1); // Uses a secure connection (HTTPS) if possible ini_set('session.cookie_secure', 1); </code></pre> <hr> <pre><code>session_start(); // If the user is already logged if (isset($_SESSION['uid'])) { // If the IP or the navigator doesn't match with the one stored in the session // there's probably a session hijacking going on if ($_SESSION['ip'] !== getIp() || $_SESSION['user_agent_id'] !== getUserAgentId()) { // Then it destroys the session session_unset(); session_destroy(); // Creates a new one session_regenerate_id(true); // Prevent's session fixation session_id(sha1(uniqid(microtime())); // Sets a random ID for the session } } else { session_regenerate_id(true); // Prevent's session fixation session_id(sha1(uniqid(microtime())); // Sets a random ID for the session // Set the default values for the session setSessionDefaults(); $_SESSION['ip'] = getIp(); // Saves the user's IP $_SESSION['user_agent_id'] = getUserAgentId(); // Saves the user's navigator } </code></pre> <p>So, my questions are</p> <ul> <li>do the <code>ini_set</code>'s provide enough security?</li> <li>is it okay to save the user's IP and navigator and then check it every time the page is loaded to detect a session hijack? Could this be problematic in any way?</li> <li>is the use of <code>session_regenerate_id()</code> correct?</li> <li>is the use of <code>session_id()</code> correct?</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload