0

Codeigniter 3: Switch session file to session memcached

Switch to session memcached in codeigniter 3.

By default, session codeigniter 3 stored in files. If you are using ubuntu, it will be stored at /var/lib/php/sessions

If your app running with one replica, you can implement this method well. But think if your app running with two replica, how do you manage the session? The session will be stored in each replica/server (redundant). This will be a problem. When you logged in to the app (in first replica), and the next time you are in second replica, you have to log in to the app again (because the session only exist in first replica, not in second replica).

So, when you are working with many replica, you have to centralize the session. Create a session server like this:

codeigniter 3 switch to session memcached

As you can see above, each app within the replica will connect to the session server over network. The app will hit session server by IP 172.55.21.33, port 11211.

Install php-memcached module

To make your app working with memcached, you have to install php-memcached module in each replica/server. Run this command in terminal:

apt-get install php-memcached

or install with specific php version

apt-get install php7.3-memcached

After installing memcached module, you can check by:

php -m

Restart webserver

We have to restart webserver to reload php module. I am using apache here. So i run the following command:

sudo service apache2 restart

Load session libraries in autoload.php

Don’t forget to load session libraries in config/autoload.php

$autoload['libraries'] = array('session');

Configure session memcached in config.php

Assume we already have session server with IP 172.55.21.33 and port 11211.

Then, we need to configure session memcached in config/config.php. Just change sess_driver to memcached and sess_save_path to 172.55.21.33:11211.

$config['sess_driver'] = 'memcached';
$config['sess_save_path'] = "172.55.21.33:11211";

Test creating a new session memcached

Now it’s time to test session memcached. I am gonna use default controller (Welcome.php) to create a new session and capture session_id

Welcome.php

public function create_session() {
	$this->session->set_userdata(array('isLogin'=>true,'date'=>'2023-08-17'));
	echo session_id(); // ui5cips8l4cv5m25c9j9s4eqjv
}

We created new session with 2 contents, isLogin and date. And the next line we print out session_id.

Every session created, it will be creating session_id and stored in session server.

Is the session already stored in the session server right now?

Previously, we have known session_id.

Now lets find those contents (isLogin and date) by session_id in the session server. We can leverage telnet command to connect to session/memcached server and checking it.

telnet 172.55.21.33 11211
get ci_session:ui5cips8l4cv5m25c9j9s4eqjv
codeigniter 3 switch to session memcached

If the response shown as above, that means session server is working well. Nice.

Ambar Hasbiyatmoko

Hello, I'm web developer. Passionate about programming, web server, and networking.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.