开发者

How to set cookies for uuid

开发者 https://www.devze.com 2023-02-28 04:14 出处:网络
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 cookies. Does anyone know of a sc

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 cookies. Does anyone know of a script which can h开发者_开发技巧elp me?


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消