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条)

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口