简体   繁体   English

codeigniter ajax请求与会话

[英]codeigniter ajax request with session

cant get session values through ajax request 无法通过ajax请求获取会话值

this is my ajax req 这是我的ajax req

$.post(url, payload, 
    function(returnedData){
        console.log(returnedData); //does not print session values it seems session has restarted
         var jsonData = JSON.parse(returnedData);

         $('#cart').empty().append(jsonData.data);
});

output in console.log 在console.log中输出

    Array
(
    [__ci_last_regenerate] => 1473660791
)

assigning session variable 分配会话变量

public function test($id=null)
{
    $_SESSION['tot']=1;
}

testing not using ajax 测试不使用ajax

public function addToCart($id=null) //note that this is also the control for my ajax req
{            
        print_r($_SESSION);
        die('0');
}
public function test1()
{
    print_r($_SESSION); // it prints fine
}

output 产量

Array ( [__ci_last_regenerate] => 1473660731 [tot] => 1 )

I autoload my session library. 我自动加载了我的会话库。 CI says it doesnt matter on how you access session variables either using session magic or the php as long as it exist. CI表示,只要它存在,使用会话魔法或php访问会话变量的方式并不重要。

Hello can you use please default codeignitor session and please mention which url you make ajax request and php code of ajax request 你好,你可以使用默认的codeignitor会话,请提一下你做的ajax请求和ajax请求的php代码的url

like 喜欢

$this->session->set_userdata('tot',1);

and then print the session array like this 然后像这样打印会话数组

echo "<pre">;print_r($this->session->userdata('tot'));

Your code should be 你的代码应该是

 public function test($id=null)
    {
        $this->session->set_userdata('tot', 1);
        //instead $_SESSION['tot']=1;
    }
    public function addToCart($id=null) //note that this is also the control for my ajax req
    {            
            $session_id = $this->session->userdata('session_id');
echo $session_id;
    }
    public function test1()
    {
        //print_r($_SESSION); // it prints fine
$session_id = $this->session->userdata('session_id');
echo $session_id;
    }

and make sure session library loaded ($this->load->library('session');) 并确保加载会话库($ this-> load-> library('session');)

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

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