转 memcache保存php的session 解决多服务器的session有关问题
转 memcache保存php的session 解决多服务器的session问题
?
?
PECL :: Package :: memcache 2.1.1 版本的 Changelog 中有一条:
- Added experimental session storage support. You can use memcached as session storage.
也就是可以直接用 memcache 来作 PHP 的 session.save_handler。
1. 安装 memcached,还是那套 ./configure; make; make install,注意 memcached 用 libevent 来作事件驱动,所以要先安装有 libevent。
2. 安装 pecl::memcache,用 pecl 命令行工具安装:
pecl install memcache
或直接从源码安装:
phpize
./configure
make
make install
将 php.ini 中 extension=memcache.so 打开,重启一下 apache,查看 phpinfo 中的 Registered save handlers 会有 files user memcache 这3个可用。
3. 修正配置文件,在 php.ini 中全局设置:
session.save_handler = memcache
session.save_path = tcp://127.0.0.1:11211
或者某个目录下的 .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);?
应用多个 memcached server 时用逗号,隔开,并且和 Memcache::addServer() 文档中阐明的一样,可以带额外的参数persistent、weight、timeout、retry_interval 等等,类似这样的:tcp://host1:port1?persistent=1&weight=2,tcp://host2:port2 。
4. 启动 memcached:
memcached -d -l 127.0.0.1 -p 11212 -m 128
5. 在程序中应用 memcache 来作 session 存储,用例子测试一下:
___FCKpd___6
6. 用 sessionid 去 memcached 里查询一下:
$memcache = memcache_connect(localhost, 11211);
var_dump($memcache->get(19216821213c65cedec65b0883238c278eeb573e077));
SESSION[TEST3] = time();
print
;
print session_id();
?>
会有看到
string(37) TEST|i:1177556731;TEST3|i:1177556881;
这样的输出,证明 session 正常工作。
用 memcache 来存储 session 在读写速度上会比 files 时快很多,而且在多个服务器须要共用 session 时会比较方便,将这些服务器都配置成应用同一组 memcached 服务器就可以,减少了额外的工作量。缺点是 session 数据都保存在 memory 中,持久化方面有所欠缺,但对 session 数据来说也不是很大的问题。

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools
