简体   繁体   English

如何设置Cookie以及该Cookie是否已存在或不执行任何操作

[英]How to set a cookie & if that cookie already exists or not do something

How to do something like this with cookies? 如何使用Cookie做类似的事情?

If a cookie exists, redirect user to a last used URL on a site or a homepage. 如果存在cookie, 则将用户重定向到网站或主页上的最后使用的URL。

If not , create a cookie + redirect to specific page with registration form. 如果不是 ,请创建一个Cookie,然后使用注册表格重定向到特定页面。

Can you check if this php -code is doing this as defined above? 您可以检查此php -code是否按照上述定义执行此操作吗?

<?php
if(!isset($_COOKIE['firsttime'])) {
?>
<meta http-equiv="refresh" content="0; url=http://example.com/">
<?php
} else {
setcookie('firsttime',time() + (86400 * 7));
?>
<meta http-equiv="refresh" content="0; url=http://example.com/"> // redirecrting to registratio form
<?php
}
?>

You should consider to use a php request rather than an meta tag because its more seamless for the end use. 您应该考虑使用php请求而不是meta标记,因为它对于最终用途更加无缝。 Check out this Thread: https://stackoverflow.com/a/768472/4043299 查看此线程: https : //stackoverflow.com/a/768472/4043299

If you want more then user redircetion eg store login of user you should use PHP Sessions . 如果您想要更多,则需要重新分配用户名(例如,存储用户的登录名),您应该使用PHP Sessions With PHP Sessions you can safely storage data which is assigned to the users active ssesion. 使用PHP会话,您可以安全地存储分配给用户活动会话的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM