How to set cookies for uuid

后端 未结 2 901
南方客
南方客 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.

提交回复
热议问题