CodeIgniter Forums
What is the best practice to unset session variable in codeigniter? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Best Practices (https://forum.codeigniter.com/forum-12.html)
+--- Thread: What is the best practice to unset session variable in codeigniter? (/thread-68763.html)



What is the best practice to unset session variable in codeigniter? - curiousteam - 08-22-2017

Hi,

What is the best practice to unset session variable in codeigniter?

Option 1: unset($_SESSION['some_variable']);

Option 2: $this->session->unset_userdata('some_variable');

Will anybody suggest me, what is the best and why?


RE: What is the best practice to unset session variable in codeigniter? - dave friend - 08-22-2017

Don't think an unqualified "best" can be determined.

If you look at the source code for unset_userdata you will see it calls unset($_SESSION['some_variable']);

The advantage to unset_userdata is that you can provide an array of sessions keys to be removed all at once without any further work on your part. In general I'll call unset on $_SESSION if a single item is being removed.


RE: What is the best practice to unset session variable in codeigniter? - curiousteam - 08-22-2017

(08-22-2017, 01:26 PM)daveĀ friend Wrote: Don't think an unqualified "best" can be determined.

If you look at the source code for unset_userdata you will see it calls unset($_SESSION['some_variable']);

The advantage to unset_userdata is that you can provide an array of sessions keys to be removed all at once without any further work on your part. In general I'll call unset on $_SESSION if a single item is being removed.

Thank you very much for your kind reply.