PHP实现多服务器session共享之memcache共享
使用基于文件的Session存取瓶颈可能都是在磁盘IO操作上,所以对付小数据量的Session没有问题,但是如果碰到大数据量的Sesstion,那么可能无法胜任,现在利用Memcache来保存Session数据,直接通过内存的方式,效率自然能够提高不少
首先打开php.ini文件,找到session的部分:(分号后面的是注释)
[Session]
; Handler used to store/retrieve data.
session.save_handler = files ;
这个是session的方式,默认的files就可以了,代表用文件储存,
还有两种方式,user和memcache。
user方式指的是你自己(也就是 用户)定义session的句柄,用于session的存取等 ,这个可以把session扩展存到数据库里
memcache方式,需要你配置好memcache ,还要配置session.save_path。
用memcache来作PHP 的session.save_handler
ini_set("session.save_handler", "memcache"); ini_set("session.save_path", "tcp://127.0.0.1:11211,tcp://192.168.1.12:11211");
用memcached 来作PHP 的session.save_handler
ini_set("session.save_handler","memcached"); ini_set("session.save_path","127.0.0.1:11211");
PHP实现多服务器session共享之memcache共享
再自定义一套session处理机制,关于session的实现方法我就不再多讲,直接贴程序了。
<?php /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */ //=========================================== // 程序: Memcache-Based Session Class // 功能: 基于Memcache存储的 Session 功能类 // 作者: yejr // 网站: http://imysql.cn // 时间: 2007-01-05 //=========================================== /** * 文件: MemcacheSession.inc.php * 类名: MemcacheSession Class * 功能: 自主实现基于Memcache存储的 Session 功能 * 描述: 这个类就是实现Session的功能,基本上是通过 * 设置客户端的Cookie来保存SessionID, * 然后把用户的数据保存在服务器端,最后通过 * Cookie中的Session Id来确定一个数据是否是用户的, * 然后进行相应的数据操作 * * 本方式适合Memcache内存方式存储Session数据的方式, * 同时如果构建分布式的Memcache服务器, * 能够保存相当多缓存数据,并且适合用户量比较多并发比较大的情况 * * 注意: 本类必须要求PHP安装了Memcache扩展或者必须有Memcache的PHP API * 获取Memcache扩展请访问: http://pecl.php.net */ //设定 SESSION 有效时间,单位是 秒 define('SESS_LIFTTIME', 3600); //定义memcache配置信息 define('MEMCACHE_HOST', 'localhost'); define('MEMCACHE_PORT', '10000'); if (!defined('MemcacheSession')) { define('MemcacheSession', TRUE); class MemacheSession { // {{{ 类成员属性定义 static $mSessSavePath; static $mSessName; static $mMemcacheObj; // }}} // {{{ 初始化构造函数 /** * 构造函数 * * @param string $login_user 登录用户 * @param int $login_type 用户类型 * @param string $login_sess 登录Session值 * @return Esession */ public function __construct() { //我的memcache是以php模块的方式编译进去的,可以直接调用 //如果没有,就请自己包含 Memcache-client.php 文件 if (!class_exists('Memcache') || !function_exists('memcache_connect')) { die('Fatal Error:Can not load Memcache extension!'); } if (!empty(self::$mMemcacheObj) && is_object(self::$mMemcacheObj)) { return false; } self::$mMemcacheObj = new Memcache; if (!self::$mMemcacheObj->connect(MEMCACHE_HOST , MEMCACHE_PORT)) { die('Fatal Error: Can not connect to memcache host '. MEMCACHE_HOST .':'. MEMCACHE_PORT); } return TRUE; } // }}} /** {{{ sessOpen($pSavePath, $name) * * @param String $pSavePath * @param String $pSessName * * @return Bool TRUE/FALSE */ public function sessOpen($pSavePath = '', $pSessName = '') { self::$mSessSavePath = $pSavePath; self::$mSessName = $pSessName; return TRUE; } // }}} /** {{{ sessClose() * * @param NULL * * @return Bool TRUE/FALSE */ public function sessClose() { return TRUE; } // }}} /** {{{ sessRead($wSessId) * * @param String $wSessId * * @return Bool TRUE/FALSE */ public function sessRead($wSessId = '') { $wData = self::$mMemcacheObj->get($wSessId); //先读数据,如果没有,就初始化一个 if (!empty($wData)) { return $wData; } else { //初始化一条空记录 $ret = self::$mMemcacheObj->set($wSessId, '', 0, SESS_LIFTTIME); if (TRUE != $ret) { die("Fatal Error: Session ID $wSessId init failed!"); return FALSE; } return TRUE; } } // }}} /** {{{ sessWrite($wSessId, $wData) * * @param String $wSessId * @param String $wData * * @return Bool TRUE/FALSE */ public function sessWrite($wSessId = '', $wData = '') { $ret = self::$mMemcacheObj->replace($wSessId, $wData, 0, SESS_LIFTTIME); if (TRUE != $ret) { die("Fatal Error: SessionID $wSessId Save data failed!"); return FALSE; } return TRUE; } // }}} /** {{{ sessDestroy($wSessId) * * @param String $wSessId * * @return Bool TRUE/FALSE */ public function sessDestroy($wSessId = '') { self::sessWrite($wSessId); return FALSE; } // }}} /** {{{ sessGc() * * @param NULL * * @return Bool TRUE/FALSE */ public function sessGc() { //无需额外回收,memcache有自己的过期回收机制 return TRUE; } // }}} /** {{{ initSess() * * @param NULL * * @return Bool TRUE/FALSE */ public function initSess() { //不使用 GET/POST 变量方式 ini_set('session.use_trans_sid', 0); //设置垃圾回收最大生存时间 ini_set('session.gc_maxlifetime', SESS_LIFTTIME); //使用 COOKIE 保存 SESSION ID 的方式 ini_set('session.use_cookies', 1); ini_set('session.cookie_path', '/'); $domain = '.imysql.cn'; //多主机共享保存 SESSION ID 的 COOKIE ini_set('session.cookie_domain', $domain); //将 session.save_handler 设置为 user,而不是默认的 files session_module_name('user'); //定义 SESSION 各项操作所对应的方法名: session_set_save_handler( array('MemacheSession', 'sessOpen'), //对应于静态方法 My_Sess::open(),下同。 array('MemacheSession', 'sessClose'), array('MemacheSession', 'sessRead'), array('MemacheSession', 'sessWrite'), array('MemacheSession', 'sessDestroy'), array('MemacheSession', 'sessGc') ); session_start(); return TRUE; } // }}} }//end class }//end define $memSess = new MemacheSession; $memSess->initSess(); ?>
?然后,在项目程序的头文件中直接包含 MemacheSession.inc.php 即可,并且以前的程序不用做任何改动。
测试 创建一个session
<?php //set_session.php session_start(); if (!isset($_SESSION['admin'])) { $_SESSION['TEST'] = 'wan'; } print $_SESSION['admin']; print "/n"; print session_id(); ?>
用 sessionid 去 memcached 里查询一下
<?php //get_session.php $mem = new Memcache; $mem->connect("127.0.0.1", 11211); var_dump($mem->get('0935216dbc0d721d629f89efb89affa6')); ?>?
备注:memcache PECL 未来版本中,可以直接设置 php.ini 来这定自己的 session.save_handler,大致如下:
session.save_handler = memcache session.save_path = "tcp://host:port?persistent=1&weight=2&timeout=2&retry_interval=15,tcp://host2:port2"


Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.


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

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
