'How to make a cookie, that is random, and won't change once the user comes back (can't be PHPSESSID= because im running node.js)
I'm trying to create a cookie that acts like a PHPSESSID but is JavaScript, I would just use PHP but i'm running node so that is impossible. The hope is that you get a 7 long character id that stays even when you reload the page and can't be replicated. This is what I have tryed but it creates a new one instead of keeping it every time.
function randomString() {
//define a variable consisting alphabets in small and capital letter
var characters = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
//specify the length for the new string
var lenString = 7;
var randomstring = '';
//loop to select a new character in each iteration
for (var i=0; i<lenString; i++) {
var rnum = Math.floor(Math.random() * characters.length);
randomstring += characters.substring(rnum, rnum+1);
}
var thing = randomstring;
document.cookie = `${thing};expires=4000;`
}
Solution 1:[1]
Ok so I just tryed the domain=https://example.com/; and that worked, maybe theres a better way feel free to post other answers! fullcode,
function randomString() {
//define a variable consisting alphabets in small and capital letter
var characters = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
//specify the length for the new string
var lenString = 7;
var randomstring = '';
//loop to select a new character in each iteration
for (var i=0; i<lenString; i++) {
var rnum = Math.floor(Math.random() * characters.length);
randomstring += characters.substring(rnum, rnum+1);
}
var thing = randomstring;
document.cookie = `${thing};expires=4000;domain=https://eez.penguinpowers.repl.co;`
}
Solution 2:[2]
express-session, It's basically PHPSessionID but for Express.js
(link)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | penguinpowerss |
| Solution 2 | Zastix |
