Welcome Guest, Not a member yet? Register   Sign In
Basic session Problem

I am facing very simple problem of session.How will i set session in CI.In general php we use..


I want to enable session for userid when he logged in.

What is the process to enable sesion in CI.Plz explain..


Have you read the sessions page in the user guide?

If so, you should have a pretty good idea, and if you still can't get it working, you can show us what you got so far, and what does/doesn't happen

Yes i read it properly and here is my implementation

if($query->num_rows() > 0 ){

$row = $query->row();

$this->session->set_userdata($id); // Need to understand here //

$session_id = $this->session->userdata('session_id'); // Need to understand here

echo $session_id;
echo "aaaaaaaaaaaaaaaaaaaaaaaa";


I want to enable session when if statement will be true.
Am i doing ok?


You're putting a session_id into the session, which is a bit confusing for me as I'd expect CI to manage that for you. On the other hand, I have no idea what your application is doing!

However, going on a very thin amount of information, I think the point that has been missed is that you can store any amount of information in the session, against a key that you decide. CI manages matching up the correct session with the correct user, timeouts etc.

So, as pointed out by n0xie, you need to specify a key with which to store the data (called 'session_id' here). You can create as many items as you like by calling this function several times with different keys.

$this->session->set_userdata('username', 'Fred');

Then, as shown in your own code, you retrieve the data by passing the key to the userdata() function.

$session_id = $this->session->userdata(‘session_id’); // correct! $session_id will be $id from before
$username = $this->session->userdata(‘username’); // $username will be 'Fred'

Note: Cookies are small and, despite their limits, will affect speed and security of your website depending on what you put in them. If you want to store any data structures or important information then I'd recommend setting up DB storage, which is quite well explained in the user guide.

Theme © iAndrew 2016 - Forum software by © MyBB