doudao1922 2015-07-17 16:38
浏览 43

CodeIgniter中未更新会话值

I'm wondering if did I just found a bug or the amount of hours programming might not let me see the obvious?

Here's an example of my code:

class home extends CI_Controller
{
    public function __construct()
    {

    }

    public function edit()
    {
        // Prints: John
        print_r($this->session->userdata('name'));

        $this->load->view('home/edit');
    }

    public function ajax_edit()
    {
        $this->session->userdata('name', 'Obama');

        // Prints: Obama
        print_r($this->session->userdata('name'));
    }
}

I call the function ajax_edit by ajax, and the print is "Obama", which is correct. After that, I refresh the page (to the edit function) and the output of the name is "John".

Why in god's heaven isn't the session updated with the value "Obama"? It seems that after refreshing it resets or something and backs to the old value "John".

  • 写回答

1条回答 默认 最新

  • douweibiao8471 2015-07-17 18:58
    关注

    The problem is that you are not using the right methods.

    In order to set any new information to your CI session, you need to use set_userdata(), and in order to retrieve, or to check, whats in the session you use userdata("key").

    Solution: Rewrite your ajax_edit() method code like so:

    public function ajax_edit() {
        $this->session->set_userdata('name', 'Obama');
    
        // Prints: Obama
        print_r($this->session->userdata('name'));
    }
    

    Source: http://www.codeigniter.com/user_guide/libraries/sessions.html#adding-session-data

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)