Where can the session be better?

Source: Internet
Author: User
Tags php session unix domain socket
Dear friends, where is the session stored better? Dear friends, where is the session stored better?

Reply content:

Dear friends, where is the session stored better?

We recommend that you put a SessionID in the Cookie and store the information on the server.
The server can put Redis, and the data has high requirements on read performance, but the stability requirement is not high.

On a machine.
For multiple machines in a project, you need to use multiple web access methods. The simplest option is shared storage NFS and Redis. MySQL can also be used if the traffic is small.
If multiple machines in multiple projects are isolated from each other, synchronous login is required, which is a little complicated and requires SSO.

In fact, it mainly depends on application scenarios, and there is no need for over-design.

We also put it in redis, and we can share sessions with multiple hosts. the configuration method is as follows:
(1) currently nginx + fastcgi + php5-fpm parsing php
(2) modify/etc/php5/fpm/php. ini
Session. save_path = "tcp: // your_ip: your_port? Auth = yourpasswd"

See phpredis already has instructions, phpredis website: https://github.com/nicolasff/phpredis...

Reference: https://github.com/nicolasff/phpredis...
Note:

PHP Session handler

Phpredis can be used to store PHP sessions. To do this, configure session. save_handler and session. save_path in your php. ini to tell phpredis where to store the sessions:

Session. save_handler = redis
Session. save_path = "tcp: // host1: 6379? Weight = 1, tcp: // host2: 6379? Weight = 2 & timeout = 2.5, tcp: // host3: 6379? Weight = 2"

Session. save_path can have a simple host: port format too, but you need to provide the tcp: // scheme if you want to use the parameters. The following parameters are available:

    weight (integer): the weight of a host is used in comparison with the others in order to customize the session distribution on several hosts. If host A has twice the weight of host B, it will get twice the amount of sessions. In the example, host1 stores 20% of all the sessions (1/(1+2+2)) while host2 and host3 each store 40% (2/1+2+2). The target host is determined once and for all at the start of the session, and doesn't change. The default weight is 1.    timeout (float): the connection timeout to a redis host, expressed in seconds. If the host is unreachable in that amount of time, the session storage will be unavailable for the client. The default timeout is very high (86400 seconds).    persistent (integer, should be 1 or 0): defines if a persistent connection should be used. (experimental setting)    prefix (string, defaults to "PHPREDIS_SESSION:"): used as a prefix to the Redis key in which the session is stored. The key is composed of the prefix followed by the session ID.    auth (string, empty by default): used to authenticate with the server prior to sending commands.    database (integer): selects a different database.

Sessions have a lifetime expressed in seconds and stored in the INI variable "session. gc_maxlifetime ". you can change it with ini_set (). the session handler requires a version of Redis with the SETEX command (at least2.0 ). phpredis can also connect to a unix domain socket: session. save_path = "unix: // var/run/redis. sock? Persistent = 1 & weight = 1 & database = 0.

View your server architecture and system requirements.
If there is only one server, you can store it anywhere. if there are multiple web servers at the front end, you need a server dedicated to session storage. you can choose a lot from what to use, such as redis, memcache, mysql, or others.

Memcache is also a good way to use it. it mainly supports distributed systems ~~~~ One or more servers are supported.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.