Home php教程 PHP源码 PHP网站session共享几种方案

PHP网站session共享几种方案

Jun 08, 2016 pm 05:21 PM
memcache save session

session共享问题原因于自己几个不同站之间需要实现session共享了,下面小编整理了一些session共享文章,希望对大家有帮助。

<script>ec(2);</script>

使用lvs或者nginx进行web的负载均衡时,一般都会遇到session共享的问题,因为一般PHP网站的session是以文件的形式存储于服务器本地的硬盘上。多个服务器进行负载均衡时,将会出现用户请求被分配到其他服务器上后session丢失的问题。解决方案主要有以下几个思路。

1.将session存储在共享的设备上,例如NFS等文件系统上

这种解决方案很容易实现,无需做特殊设置,将nfs目录mount至php的session存储目录即可。缺点是NFS依托于复杂的安全机制和文件系统,因此并发效率不高,尤其对于session这类高并发读写的小文件, 会由于共享目录服务器的io-wait过高,最终拖累前端WEB应用程序的执行效率。

2. 基于数据库的Session共享

首选当然是大名鼎鼎的Mysql数据库,并且建议使用内存表Heap,提高session操作的读写效率。这个方案的实用性比较强,相信大家普遍在使用,它的缺点在于session的并发读写能力取决于Mysql数据库的性能,同时需要自己实现session淘汰逻辑,以便定时从数据表中更新、删除session记录,当并发过高时容易出现表锁,虽然我们可以选择行级锁的表引擎,但不得不否认使用数据库存储Session还是有些杀鸡用牛刀的架势

3. 基于Memcache的Session共享

Memcache由于是一款基于Libevent多路异步I/O技术的内存共享系统,简单的Key + Value数据存储模式使得代码逻辑小巧高效,因此在并发处理能力上占据了绝对优势。
另外值得一提的是Memcache的内存hash表所特有的Expires数据过期淘汰机制,正好和Session的过期机制不谋而合,降低了过期Session数据删除的代码复杂度,对比“基于数据库的存储方案”,仅这块逻辑就给数据表产生巨大的查询压力
 
1.首先安装memcache,以及memcache在php中的扩展
2.修改php.ini文件

 代码如下 复制代码

session.save_handler = memcache
session.save_path = “tcp://127.0.0.1:11211″

使用多个 memcached server 时用逗号”,”隔开,并且和 Memcache::addServer() 文档中说明的一样,可以带额外的参数”persistent”、”weight”、”timeout”、”retry_interval” 等等,类似这样的:”tcp://host1:port1?persistent=1&weight=2,tcp://host2:port2″

3.大功告成,重启apache和memcache看看phpinfo里的session配置是不是变成了memcache了。
4.自己的程序完全不用修改,系统已经自己去找memcache来做他的session宿主啦。
PS:如果不想整台服务器全局都是用memcache的话也可以用如下两种方法:

 代码如下 复制代码

修改 .htaccess

php_value session.save_handler “memcache”
php_value session.save_path “tcp://127.0.0.1:11211″

修改程序

ini_set(“session.save_handler”,”memcache”);
ini_set(“session.save_path”, “tcp://127.0.0.1:11211″);

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve session failure How to solve session failure Oct 18, 2023 pm 05:19 PM

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Solution to PHP Session cross-domain problem Solution to PHP Session cross-domain problem Oct 12, 2023 pm 03:00 PM

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

How to use Memcache in PHP development? How to use Memcache in PHP development? Nov 07, 2023 pm 12:49 PM

In web development, we often need to use caching technology to improve website performance and response speed. Memcache is a popular caching technology that can cache any data type and supports high concurrency and high availability. This article will introduce how to use Memcache in PHP development and provide specific code examples. 1. Install Memcache To use Memcache, we first need to install the Memcache extension on the server. In CentOS operating system, you can use the following command

What are the differences between JavaScript and PHP cookies? What are the differences between JavaScript and PHP cookies? Sep 02, 2023 pm 12:29 PM

JavaScriptCookies Using JavaScript cookies is the most effective way to remember and track preferences, purchases, commissions and other information. Information needed for a better visitor experience or website statistics. PHPCookieCookies are text files that are stored on client computers and retained for tracking purposes. PHP transparently supports HTTP cookies. How do JavaScript cookies work? Your server sends some data to your visitor's browser in the form of a cookie. Browsers can accept cookies. If present, it will be stored on the visitor's hard drive as a plain text record. Now, when a visitor reaches another page on the site

PHP Session cross-domain and AJAX asynchronous communication optimization PHP Session cross-domain and AJAX asynchronous communication optimization Oct 12, 2023 am 09:22 AM

Optimization of asynchronous communication between PHPSession across domains and AJAX With the development of the Internet, cross-domain access and asynchronous communication have become common requirements in modern web application development. This article will focus on how to use PHPSession to achieve cross-domain access, and provide some optimization methods to improve the asynchronous communication efficiency of AJAX. 1. The problem of cross-domain access In Web development, when the browser initiates an HTTP request from a web page of one domain name, and then returns the response data belonging to another domain name, it will occur.

Amazfit Helio Ring now available in Europe: Save €149 when bundled with a smartwatch Amazfit Helio Ring now available in Europe: Save €149 when bundled with a smartwatch Jun 17, 2024 am 11:19 AM

Amazfitunveileditsfirstsmartring,theHelio,atCES2024earlierthisyear.Almostsixmonthslater,itisnowbeingsoldinEuropeaswell.Accordingtothemanufacturer,thenewAmazfitHelioRingiscurrentlyavailableandcanbeordereddirectly

How to use Memcache for efficient data reading and writing operations in PHP development? How to use Memcache for efficient data reading and writing operations in PHP development? Nov 07, 2023 pm 03:48 PM

In PHP development, using the Memcache caching system can greatly improve the efficiency of data reading and writing. Memcache is a memory-based caching system that can cache data in memory to avoid frequent reading and writing of the database. This article will introduce how to use Memcache in PHP for efficient data reading and writing operations, and provide specific code examples. 1. Install and configure Memcache First, you need to install the Memcache extension on the server. able to pass

What are the reasons for session failure? What are the reasons for session failure? Oct 17, 2023 pm 05:01 PM

Reasons for session failure include session timeout, session number limit, session integrity check, server restart, browser or device problems, etc. Detailed introduction: 1. Session timeout: The server sets a default timeout for the Session. When the user does not interact with the server for a period of time, the Session will automatically expire; 2. Session number limit: The server has a number of Sessions for each user. A limit is set. When the number of Sessions created by a user exceeds this limit, the latest one will overwrite the oldest one and so on.

See all articles