Welcome Guest, Not a member yet? Register   Sign In
isset($_SESSION['foo']) vs $this->session->has_userdata('foo')
#1

Hello,

I'm just starting to use CI, and I'm wondering what's the good way of dealing with session.

I wrote things like that so far :

PHP Code:
$this->session->foo 'bar';
...
if (
$this->session->foo == 'bar')
...
if (
$this->session->has_userdata('foo'))
... 

But in the doc, there is a note :
Quote:This is a legacy method kept only for backwards compatibility with older applications. It is just an alias for isset($_SESSION[$key]) - please use that instead.

I'm not sure to understand why the use of has_userdata is not recommended. Why can I use a magic object "session" to set and retrieve data from the session, and why I should access directly to the plain vanilla $_SESSION array to test if a key exists.

Could someone explain me what should I use ?
Thanks !
Reply
#2

You've already quoted the answer - backwards compatibility.
Reply
#3

Should i use in next project or not??
Reply
#4

@Narf
IMO $this->session->has_userdata('foo') and the other methods should not be deprecated in the future. It is a good abstration.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB