简体   繁体   中英

codeigniter ajax request with session

cant get session values through ajax request

this is my 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

    Array
(
    [__ci_last_regenerate] => 1473660791
)

assigning session variable

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

testing not using 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.

Hello can you use please default codeignitor session and please mention which url you make ajax request and php code of ajax request

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');)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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