How to set cookies for uuid

跟風遠走 提交于 2020-01-06 12:52:46

问题


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 help me?


回答1:


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.




回答2:


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
}


来源:https://stackoverflow.com/questions/5751194/how-to-save-uuid-result-with-cookies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!