简体   繁体   English

Codeigniter会话类的问题

[英]Problems with Codeigniter Session Class

this is my problem... I added to the construct of CI_Controller these lines 这是我的问题...我将这些行添加到CI_Controller的构造中

    $this->load->library("session");
    if($this->session->userdata("var")==FALSE)
    {
        $this->session->set_userdata("var", "value");   
    }

And when I use $this->session->userdata("var") in a child class, it doesn't have a value. 当我在子类中使用$ this-> session-> userdata(“ var”)时,它没有值。 I tested that by showing the value of $this->session->userdata("var") in the construct of CI_COntroller and in the construct of the child class and it shows me the value on the father but no on the child. 我通过在CI_COntroller的构造和子类的构造中显示$ this-> session-> userdata(“ var”)的值进行了测试,它向我展示了父亲的价值,而不是孩子的价值。

There is another thing, after reload the page about 3 or 4 times. 还有另一件事,在重新加载页面大约3或4次之后。 It works very well. 效果很好。

I will be grateful for your answers. 谢谢您的答复。 (And I'm sorry I'm not an English speaker) (对不起,我不会说英语)

you should do 你应该做

$this->load->library("session");
    if(!$this->session->userdata("var"))
    {
        $this->session->set_userdata("var", "value");   
    }

cause userdata() method returns false and true if isset or not session data. 原因userdata()是否设置了会话数据,所以userdata()方法返回false和true。

You could have done it like this 你可以这样做

$this->load->library("session");

$var    =   $this->session->userdata("var");

if(!$var)
{
    $this->session->set_userdata("var", "value");   
}

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

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