浏览器退出时 Cookie 被销毁
我目前正在我的网站上使用 cookie,我做的第一件事是检查用户是否有 cookie,如果没有,我会显示一个带有 3 个选项的菜单,如果他们单击它会创建一个 cookie,但如果然后退出浏览器中的 cookie 被销毁,这是我的代码,
function createRandomId() {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime() * 1000000);
$i = 0;
$unique = '';
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$unique = $unique.$tmp;
$i++;
}
return md5($unique);
}
function index() {
// $data is the array of data that is passed to views, setup it up
$data = array();
// We need to setup the cookie that will be used site, this will be used to cross reference
// The user with the options they have selected, to do this we first need to load the session model
// Check if the user has a cookie already, if they it means they have been to the site in the last 30 days.
if(!isset($_COOKIE['bangUser'])) {
// Get createRandomId() method and return a unique ID for the user
$unique = '';
// Setting the cookie, name = bangUser, the cookie will expire after 30 days
setcookie("bangUser", $unique, time() + (60*60*24*30));
$data['firstTime'] = TRUE;
} else {
$data['notFirstTime'] = TRUE;
}
// Load the view and send the data from it.
$this->load->view('base/index', $data);
}
function createCookie() {
// Function gets called when the user clicks yes on the firstTime menu.
// The purpose of this function is to create a cookie for the user.
// First we'll give them a unique ID
$unique = $this->createRandomId();
// With the unique ID now available we can set our cookie doing the same function as before
setcookie("bangUser", $unique, time() + (60*60*24*30));
// Now that the cookie is set we can do a 100% check, check that cookie is set and if it is redirect to
// to the homepage
if(isset($_COOKIE['bangUser'])) {
redirect('welcome');
}
}
基本上是 index() 函数进行检查,并且 createCookie 创建一个新的 cookie,任何人都可以看到任何问题吗?
I am currently playing around with cookies in my website, the first thing I do is run a check as to whether the user has cookie, if they dont I display a menu wth 3 options if they click it creates a cookie, but if then quit the browser the cookie is destroyed, here is my code,
function createRandomId() {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime() * 1000000);
$i = 0;
$unique = '';
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$unique = $unique.$tmp;
$i++;
}
return md5($unique);
}
function index() {
// $data is the array of data that is passed to views, setup it up
$data = array();
// We need to setup the cookie that will be used site, this will be used to cross reference
// The user with the options they have selected, to do this we first need to load the session model
// Check if the user has a cookie already, if they it means they have been to the site in the last 30 days.
if(!isset($_COOKIE['bangUser'])) {
// Get createRandomId() method and return a unique ID for the user
$unique = '';
// Setting the cookie, name = bangUser, the cookie will expire after 30 days
setcookie("bangUser", $unique, time() + (60*60*24*30));
$data['firstTime'] = TRUE;
} else {
$data['notFirstTime'] = TRUE;
}
// Load the view and send the data from it.
$this->load->view('base/index', $data);
}
function createCookie() {
// Function gets called when the user clicks yes on the firstTime menu.
// The purpose of this function is to create a cookie for the user.
// First we'll give them a unique ID
$unique = $this->createRandomId();
// With the unique ID now available we can set our cookie doing the same function as before
setcookie("bangUser", $unique, time() + (60*60*24*30));
// Now that the cookie is set we can do a 100% check, check that cookie is set and if it is redirect to
// to the homepage
if(isset($_COOKIE['bangUser'])) {
redirect('welcome');
}
}
Basically the index() function does the check and the createCookie creates a new cookie, can any one see any problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在你的 createCookie 函数中,调用 setCookie 不会立即将值添加到 $_COOKIE 超全局中 - 该数组仅保存发出请求时存在的 cookie(但无论如何你都可以将新的 cookie 值存储在数组中)
另外,如果你愿意浏览器退出时会销毁的会话 cookie,指定过期时间为 null。或者,只需使用 PHP 的内置会话。
In your createCookie function, calling setCookie will not immediately add the value to the $_COOKIE superglobal - this array only holds the cookies present when the request was made (but you could store your new cookie value in the array anyway)
Also, if you want a session cookie which is destroyed when the browser quits, specify null for the expiration time. Alternatively, just use PHP's built in sessions.
您需要将 setcookie ($path) 的第四个参数设置为您网站的绝对路径。例如:
You need to set the fourth parameter of setcookie ($path) to the absolute path of you're website. For example: