Welcome Guest, Not a member yet? Register   Sign In
Problem with getting userdata from codeigniter session which has been stored in DB
#1

[eluser]Unknown[/eluser]
Hi,

I just enabled saving sessions in DB:
Code:
$login_info=array('logged_in_user_id' => $user_result['u_id'],
'logged_in_username'=> $user_result['u_username']);

$this->session->set_userdata('login',$login_info); // after this the session will be stored in db but I cant have access to it..

But I have problem with this code:
Code:
if(!empty($this -> session -> userdata('login')))
{
    echo "login true";
} else {
    echo "login false"; // I always get this one!
        }

according to my above explanation:

1- is it a secure way for manipulating login process in codeigniter?
2- What should I do to get the db session data and get rid of the problem above?

Your answer is appreciated,

Warm regards
#2

[eluser]Tim Brownlaw[/eluser]
This doesn't address your two main questions, but it discusses the code you have used

It's Debug time...
When you come across these kinds of situations your first question should be...
"What is "it" coming back with..."

With "It" being $this->session->userdata('login') , in this case!

The easiest way is to display it on the browser.
So to view the item of interest you can use...
Code:
var_dump($this->session->userdata('login'));

or better yet use the following to see all the session data.
Code:
var_dump($this->session->all_userdata());

What you might be finding is that the value is never empty.
The Users Guide ( it's ALWAYS worth reading... ) states that a Non Entry will return FALSE.

So $this->session->userdata('login') will return a value if one exists OR False.
And that will be backed up by your observations of the above var_dumps...

And then you might find that this works!
Code:
if($this->session->userdata('login') === FALSE)
{
  echo "login false";
} else {
  echo "login true";
}

And don't just copy that and say Yeah it works... or no it doesn't ( I've not tested it... )
Try and Work Out what is actually happening...

It's important to understand what is going on... so in future issues like this, you should be able to figure these things out... But that takes time and experience which you are on your way to achieving...

When testing things for values like the above, you need to know what return values will be given for all cases.

And just to add, it's always very very good to use Forums just like this when you are stuck Smile
Cheers




Theme © iAndrew 2016 - Forum software by © MyBB