Reputation: 4267
I am using CodeIgniter framework at server side to provide backend to a native mobile app. At server side I have to use cart. I am using db for sessions in CodeIgniter. When we use sessions then session_id in cookie of browser let the session work. Mobile app retrieves the session ID from server response after login request is successful - then this session_id can be sent from mobile app to the server in further requests.
Now is there a way in CI to get the data of session on the basis of sessionid? Like if mobile app sends session_id 'xyz' then how can I get the data of session whose session_id is 'xyz'? And how it will be better to work with the session while using cart with it?
Following is my login code. Sessions are being stored in ci_session
table of db.
function login($username,$password){
$query = $this->db->select("id,username")->get_where(self::$table, array('username' => $username,'Password'=>$password));
$arr=$query->row_array();
if($arr){
$this->session->set_userdata(array('id'=>$arr['id'],'username'=>$arr['username']));
// $this->db->insert('ci_sessions',array('user_id'=>$arr['id'],'username'=>$arr['username']));
return $this->session->userdata('session_id');
}
return 0;
}
So how can I get session w.r.t. session_id and how can cart work with it? Because I think as in this document: http://codeigniter.com/user_guide/libraries/cart.html cart automatically stores content against loaded session. Therefore I need to load session according to session_id so I can use CIs cart library correctly.
How can I load the session based on session_id, or tell me if I am doing it in a bad way?
Upvotes: 0
Views: 1668
Reputation: 16055
First of all, the Question is very bad formatted and written down thus not very understandable...
If I understand it right, here is what You get:
Now - if You have the session stored in the database table ci_session
, You only need to request the contents of session stored in the DB table based on session ID provided, so using Your login script example I would do sth similar:
function loadSessionBySessionId($sessionId){
$query = $this->db->select("*")->get_where('ci_session', array('session_id' => $sessionId));
return $query->row_array();
}
Extend the code to meet Your coding convention and server side application. Somewhere else at the server side application where You process the request from mobile app You will check whether the session is returned or not (if no, a FALSE should be returned) and take further actions...
Don't know how Your cart is working - this part is just upon You (unless You make it clear to us what You want us to help You with Your cart).
Upvotes: 2