CodeIgniter Forums
CI 3 native session driver? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forum-11.html)
+--- Thread: CI 3 native session driver? (/thread-62854.html)

Pages: 1 2


CI 3 native session driver? - blasto333 - 09-02-2015

I understand I have 4 choices right now: file, database, memcache and redis.

Is there a way to use native Php session so I could use the built in memcached extension and not have to use ci's memcache driver?

I understand all the drivers are just custom session handlers; but I am just surprised there is not a native session driver.


RE: CI 3 native session driver? - CroNiX - 09-03-2015

The file driver is the native php session handler, which saves to files. It's just a wrapper for $_SESSION.

You can always just use $_SESSION in your application as well and not use any session library (that's about as native as you can get).


RE: CI 3 native session driver? - blasto333 - 09-03-2015

(09-03-2015, 09:18 AM)CroNiX Wrote: The file driver is the native php session handler, which saves to files. It's just a wrapper for $_SESSION.

You can always just use $_SESSION in your application as well and not use any session library (that's about as native as you can get).


It isn't really a wrapper because it uses its own file management. I want to use memcached extension which expects native php sessions. (Not managing their own files)


RE: CI 3 native session driver? - CroNiX - 09-03-2015

So just use $_SESSION if you need pure native sessions?


RE: CI 3 native session driver? - blasto333 - 09-03-2015

(09-03-2015, 09:39 AM)CroNiX Wrote: So just use $_SESSION if you need pure native sessions?

session_start() and session_name('cookie_name') need to be called every timeĀ and I want to use the CI helper methods as that is what my code uses now. In CI 2.x there was a native session 3rd party library I used; but with the new driver model there is not one that I know of that exists.


RE: CI 3 native session driver? - CroNiX - 09-03-2015

AFAIK, there simply isn't anything that exists currently that uses pure native sessions in CI. So you can't do what you are wanting to do without using native $_SESSION. Just put the session_start and session_name in index.php, where it will be called for each request. The helpers you are using are most likely very simple things that you can create your own helper that works with native sessions to replace.


RE: CI 3 native session driver? - mwhitney - 09-03-2015

What exactly do you need the memcached session driver to do which it doesn't already?

Memcached does the same thing CI3 does: it provides its own save handler for sessions. Both are equally "native" PHP sessions.


RE: CI 3 native session driver? - blasto333 - 09-03-2015

(09-03-2015, 11:52 AM)mwhitney Wrote: What exactly do you need the memcached session driver to do which it doesn't already?

Memcached does the same thing CI3 does: it provides its own save handler for sessions. Both are equally "native" PHP sessions.

I already have that setup and it supports failover. If one memcached server goes down it can use the other one without losing session data. I couldn't seem to find this in CI's documentation besides providing weight values. With memcached you can write to more than one memcached server and when it fails it just starts using the ones that are available.


RE: CI 3 native session driver? - mwhitney - 09-03-2015

If I'm reading the documentation of Memcached and the session driver code correctly, Memcached's failover support is handled by Memcached itself as long as you use the addServer() method, which CI's session driver does.

I suspect that the primary concern here would be that if, somehow, the host:port combination supplied to the sess_save_path config value was unavailable when the session was initially opened, you would not be able to access the session, even if other servers in the Memcached pool were available. However (again, if I'm reading the code correctly), I don't even believe that will be an issue, as it only appears to be checking whether the supplied host:port is in the existing list of servers in the Memcached instance and logs an error message if it fails to add the server. As long as the server list retrieved from Memcached is not empty, the open() method of the session handler returns true, so it should continue even if the host:port combination configured in sess_save_path is unavailable.

Also, regardless of which session handler you're using, there's never a guarantee that Memcached won't lose session data just because you're using server pools.


RE: CI 3 native session driver? - blasto333 - 09-03-2015

Thank you for your input. I understand I can probably get one of these to work how I want to; but I don't see the harm in making a native session driver. I don't even think it would be too hard to write. This way you wouldn't have to deal with setting up file permissions (for the file driver; as long as php is setup correctly) AND it gives you the flexibility to use other session drivers that hook into php.