Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ul> <li>A session is simply a unique key (session ID) associated with an object/array, this way you can connect data to a user of your site.</li> <li>You send the session ID to the client as a cookie, once sent, the client sends its session ID to your server with every HTTP request.</li> <li>You send the HTTP <code>Set-Cookie</code> header (<code>Set-Cookie: sessionid=abcdefg38974</code>).</li> <li>You can make it expire when you want it to, when the browser closes or after for example a year (this would keep you logged in for a year, after which you'll have to login again).</li> <li>When a cookie expires it is simply thrown away, on the serverside it will just look like the user doesn't have a session ID set yet so he has to log in again.</li> <li>You should know of things like session hijacking (stealing someone else's session ID). Have a look at it.</li> </ul> <p>Little example, it might increate the visit number twice each time you request because your browser also requests <code>/favicon.ico</code>. Keep in mind that this example is not very safe as session hijacking is possible, this also keeps all sessions in memory and forgets them when the server is restarted. It would be wise to save sessions to a database.</p> <pre class="lang-js prettyprint-override"><code>var http = require('http'); var sessions = {}; http.createServer(function (req, res) { var ssid; var session; if (req.headers.cookie) { // Cookie already set, read it var parts = req.headers.cookie.split('='); ssid = parts[1]; // Is the session id known to us? if (typeof(sessions[ssid]) != "undefined") { session = sessions[ssid]; console.log('Loaded session with id ' + ssid); } } if (typeof(session) == "undefined") { // No cookie set, make one ssid = Math.random(); session = { 'visitNumber': 0 } console.log('Created session with id ' + ssid); } session.visitNumber++; // Respond to the browser res.writeHead(200, { 'Content-Type': 'text/plain', 'Set-Cookie': 'ssid=' + ssid }); res.end('Visit number: ' + session.visitNumber + '\n'); // Save the changes we have made to the session data sessions[ssid] = session; }).listen(8080); </code></pre>
    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.
    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