CodeIgniter Forums
isset($_SESSION['foo']) vs $this->session->has_userdata('foo') - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: isset($_SESSION['foo']) vs $this->session->has_userdata('foo') (/showthread.php?tid=61520)



isset($_SESSION['foo']) vs $this->session->has_userdata('foo') - ôkio - 04-22-2015

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 !


RE: isset($_SESSION['foo']) vs $this->session->has_userdata('foo') - Narf - 04-22-2015

You've already quoted the answer - backwards compatibility.


RE: isset($_SESSION['foo']) vs $this->session->has_userdata('foo') - Vimal - 04-26-2015

Should i use in next project or not??


RE: isset($_SESSION['foo']) vs $this->session->has_userdata('foo') - ivantcholakov - 04-27-2015

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