如何制作cookie,这是随机的,一旦用户返回,就会不会更改(可以是phpsessid =,因为IM正在运行node.js)

发布于 2025-01-22 02:35:01 字数 834 浏览 2 评论 0原文

我正在尝试创建一个像phpsessID一样的曲奇,但是JavaScript,我只会使用PHP,但是我正在运行节点,所以这是不可能的。希望您获得了一个7个长角色ID,即使您重新加载页面也无法复制。这是我尝试的,但它创造了一个新的,而不是每次保留它。

 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;`
}  

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;`
}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽容 2025-01-29 02:35:01

好的,所以我刚刚尝试了domain = https://example.com/; and and and and and and and and and and and and and and and and and也许有更好的方法可以随意发布其他答案!完整代码,

  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;`
}  

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;`
}  
初熏 2025-01-29 02:35:01

明确的课程,基本上是phpsession,但对于express.js
link

express-session, It's basically PHPSessionID but for Express.js
(link)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文