How to set cookies for uuid

后端 未结 2 884
南方客
南方客 2020-11-27 08:16

I have a website which generates a uuid every time the page is loaded/refreshed. I want to make it so that a certain value remains the same for a period of time using cookie

相关标签:
2条回答
  • 2020-11-27 08:41

    Not sure why you are asking for a script, or what the problem here is. To set a cookie, just use:

    if (empty($_COOKIE["uuid"])) {
        $uuid = uniqid();  // or use a real UUID
        setcookie("uuid", $uuid, time()+30*24*60*60, "/");
    }
    else {
        $uuid = $_COOKIE["uuid"];
    }
    

    Actually you should execute the setcookie once in a while and anyway to have the cookie lifetime refreshed.

    0 讨论(0)
  • 2020-11-27 08:48

    You can set a cookie that's deleted when the browser session closes. That can be used as a signal for when the user is "revisiting" the site. Storing a uuid cookie on each page will then give you the last uuid from which you can do what you were requesting.

    setcookie('firstvisit', 1);
    setcookie('uuid', $uuid, time()+368400);
    if(isset($_COOKIE['firstvisit']) && isset($_COOKIE['uuid'])) {
        // load uuid content
    }
    
    0 讨论(0)
提交回复
热议问题