Cookie未保存 [英] Cookie not saving

查看:104
本文介绍了Cookie未保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,用户会看到一个是/否菜单,它决定了他们是否看到它。这是这段代码。

On my site the user is greeted with a yes/no menu what determines whether they see it, is this peiece of code.

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;
        }

如果用户单击是,则此次运行

If the user clicks yes then this run

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();
        // Set an expiration time
        $expireAt = time() + (60*60*24*30);
        // With the unique ID now available we can set our cookie doing the same function as before
        $_COOKIE[] = setcookie("bangUser", $unique, $expireAt);
        // 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'])) {
            // We need to save the cookie data to the database
            // First let's load the model for the cookies
            $this->load->model('cookieModel');
            $this->cookieModel->saveCookieRecord($unique, $expireAt);
            redirect('/welcome');
        }
    }

如果没有运行此代码,

 function createCookieLater() {
            // Function gets called when the user clicks yes on the firstTime menu.
            // The of this function is create a cookie for the user, but this time it,
            // It will expire when the user closes the window.
            // Again we give them an ID
            $unique = $this->createRandomId();
            $_COOKIE[] = setcookie("bangUser", $unique, 0);
            // Now that we have set the cookie, we again need to check that it is properly set,
            // and if it we can redirect the user back to the main page, again.
            if(isset($_COOKIE['bangUser'])) {
                // We need to save the cookie data to the database
                // First let's load the model for the cookies
                $this->load->model('cookieModel');
                $this->cookieModel->saveCookieRecord($unique, $expireAt);
                redirect('/welcome');
            }
        } 

希望有些人可以帮助我

推荐答案

您应该在存储cookie时真正指定域:

You should really specify the domain when storing cookies:

setcookie("bangUser", $unique, time() + (60*60*24*30));

应成为:

setcookie("bangUser", $unique, time() + (60*60*24*30), '/', '.yourdomain.com');

我很确定这是你的问题。

I'm pretty sure this is your issue.

这篇关于Cookie未保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆