douchuose2514 2013-12-30 20:35
浏览 54
已采纳

codeigniter全局变量需要由多个控制器访问

I am new to codeigniter , In my program i want a variable need to be accessed by multiple controllers, It's not a constant variable, value of variable changes ,

Sorry , My mistake I want to store a JSON object to be precise

Pls help me to figure this out.

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • drd0833 2013-12-30 21:23
    关注

    Option 1

    Since you are using CodeIgniter and sessions then something like this could work out for you:

    set it

    $someJSONobject = 'JSON';
    
    $this->session->set_userdata('GLOBAL_JSON', $someJSONobject);
    

    retrieve it

    $someJSONobject = $this->session->userdata('GLOBAL_JSON');
    
    echo $someJSONobject->subitem;
    

    Make sure you are storing sessions in a DB if you go with this option because Cookie space is VERY limited

    Option 2

    Even if you are not using CodeIgniters' session implementation then you can do something quite similar in native PHP:

    $someJSONobject = 'JSON';
    
    $_SESSION['GLOBAL_JSON'] = $someJSONobject;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?