Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no need to do what you are attempting. When you start a session in PHP with session_start() a unique SESSIONID is already generated for you. You should <em>not</em> be putting this on the form. It is handled via cookies by default. There is also no need to check the SESSIONID either, that again is handled for you. </p> <p>You are responsible for authenticating the user and storing their authenticated identity (e.g. $_SESSION['user_id'] = $userId in the SESSION. If a user logs out you destroy their session with session_destroy.</p> <p>You should ensure session_start() is one of the <em>first</em> things for all pages in your site.</p> <p>Here is a basic example:</p> <pre><code>&lt;?php session_start(); // starts new or resumes existing session session_regenerate_id(true); // regenerates SESSIONID to prevent hijacking function login($username, $password) { $user = new User(); if ($user-&gt;login($username, $password)) { $_SESSION['user_id'] = $user-&gt;getId(); return true; } return false; } function logout() { session_destroy(); } function isLoggedIn() { return isset($_SESSION['user_id']); } function generateFormHash($salt) { $hash = md5(mt_rand(1,1000000) . $salt); $_SESSION['csrf_hash'] = $hash return $hash; } function isValidFormHash($hash) { return $_SESSION['csrf_hash'] === $hash; } </code></pre> <p>Edit: I misunderstood the original question. I added the relevant methods above for generating and validating form hashes;</p> <p>Please see the following resources:</p> <ul> <li><a href="http://us3.php.net/manual/en/book.session.php" rel="noreferrer">PHP Session Handling</a></li> <li><a href="http://us3.php.net/manual/en/function.session-start.php" rel="noreferrer">session_start()</a></li> <li><a href="http://us3.php.net/manual/en/function.session-destroy.php" rel="noreferrer">session_destroy()</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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