PHP shared session

Server environment

1
2
3
4
5
$ cat /etc/issue
Ubuntu 14.04.5 LTS

$ php -v
PHP 5.6.32-1+ubuntu14.04.1+deb.sury.org+2 (cli)

Session in PHP has two problem in a web project.

  • If cookie is disable in client browser
  • How to synchronize session

Session base usage in PHP

1
2
session_start();                        //Start session
$_SESSION['name'] = "liyuliang";

Different sub-domains direct to the same server:

There are three way to do this.

  1. Add this code at the beginning of the php page (before any output and before the method session_start() )
1
2
3
ini_set('session.cookie_path', '/');
ini_set('session.cookie_domain', '.mydomain.com');
ini_set('session.cookie_lifetime', '1800');

or

1
session_set_cookie_params(1800 , '/', '.mydomain.com');

  1. Point to domain forever in php configuration file php.ini:
    1
    2
    3
    session.cookie_path = /
    session.cookie_domain = .mydomain.com
    session.cookie_lifetime = 1800

The above methods have the same effect.

Different sub-domains direct to different server:

Use NFS to share session

  • If session number is relatively large and all session files are under the same directory
  • If NFS Server get malfunction
  • If NFS network can not be connected

It may causes a serious load problem

There will be three server machine

NFS Hostname IP
Server site1 192.168.33.10
Client site2 192.168.33.11
Client site3 192.168.33.12

Step 1. Change PHP session storage directory
Step 2. Install NFS on each server machine
Step 3. NFS clients mount to the main server then share the main server ‘s session directory

Server

Use /tmp/php_sess as the session sharing directory

1
$ mkdir /tmp/php_sess

Direct php session save path to redis

1
2
3
4
5
6
7
$ sudo vim /etc/php/5.6/apache2/php.ini

session.save_handler = files
session.save_path = "/tmp/php_sess"
session.cookie_path = /
session.cookie_domain = .mydomain.com
session.cookie_lifetime = 1800

Restart apache

1
$ sudo service apache2 restart

Install NFS client

1
$ sudo apt-get install -y nfs-kernel-server

Limit NFS client connecting ip

1
2
3
$ sudo vim /etc/exports 
/tmp/php_sess 192.168.33.11(rw,sync,no_root_squash,no_subtree_check)
/tmp/php_sess 192.168.33.12(rw,sync,no_root_squash,no_subtree_check)

Restart NFS service

1
$ sudo service nfs-kernel-server restart

Client

Use /tmp/php_sess as the session sharing directory

1
$ mkdir /tmp/php_sess

Install NFS client

1
$ sudo apt install -y nfs-common

Show NFS server info

1
2
3
$ sudo showmount -e 192.168.33.10
Export list for 192.168.33.10:
/tmp/php_sess 192.168.33.12,192.168.33.11

Mount the directory

1
$ sudo mount 192.168.33.10:/tmp/php_sess /tmp/php_sess

Check NFS mount result

1
$ ls -lh /tmp/php_sess/

Direct php session save path to redis

1
2
3
4
5
6
7
$ sudo vim /etc/php/5.6/apache2/php.ini

session.save_handler = files
session.save_path = "/tmp/php_sess"
session.cookie_path = /
session.cookie_domain = .mydomain.com
session.cookie_lifetime = 1800

Restart apache

1
$ sudo service apache2 restart

Mount NFS share directory automatically as machine boot

1
2
$ sudo vim /etc/fstab
192.168.33.10:/tmp/php_sess /tmp/php_sess nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0

Different domains direct to different server:

Install redis

1
$ sudo apt-get install -y redis-server

Bind redis ip

1
2
$ sudo vim /etc/redis/redis.conf 
bind 192.168.33.10

Restart redis

1
$ sudo service redis-server restart

Install php-redis extension

1
$ sudo apt-get install -y php5.6-redis

Direct the session save path to redis in php

1
2
3
$ sudo vim /etc/php/5.6/apache2/php.ini
session.save_handler = Redis
session.save_path = "tcp://192.168.33.10:6379"

Restart apache

1
$ sudo service apache2 restart

Share