国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 利用Memcached在php下實(shí)現(xiàn)session機(jī)制 替換PHP的原生session支持

利用Memcached在php下實(shí)現(xiàn)session機(jī)制 替換PHP的原生session支持

Jun 06, 2016 pm 08:34 PM
memcached session

利用Memcached在php下實(shí)現(xiàn)session機(jī)制,替換PHP的原生session支持

方法文件
session實(shí)現(xiàn)文件:memcachedsession.php
實(shí)現(xiàn)原理(也是PHP內(nèi)部session的實(shí)現(xiàn)原理):
1.先判斷客戶端有沒有sessionid,
a.沒有就添加一個(gè)sessionid給客戶端,通常是32位hash碼,同時(shí)初始化一個(gè)數(shù)組做session容器
b.如果客戶端有sessionid,則利用這個(gè)sessionid去memcached里面查數(shù)據(jù)。
2.用戶在頁面執(zhí)行過程中可以自行修改session容器里的session值
3.頁面最后會(huì)把用戶的session容器作為值,以用戶的sessionid作為鍵,美國空間,把這個(gè)鍵值對保存到
memcached里面

復(fù)制代碼 代碼如下:


//memcached服務(wù)器連接地址
$_MEMCACHEAUTH = array(
'host' => 'localhost'
, 'port' => 11211
);
/*
獲取一些初始化設(shè)置值
*/
$_SESSION_NAME = ini_get("session.name"); //sessionid的名稱
$_SESSION_TIME = ini_get("session.cookie_lifetime"); //sessionid這個(gè)cookie的最大保存時(shí)間
$_SESSION_EXPIRE = ini_get("session.gc_maxlifetime"); //session鍵值對在memcached里面的過期時(shí)間
$_SESSION_MEMKEY = ""; //sessionid值
/*
自定義的_session_start()方法,香港服務(wù)器租用,替換PHP的原生session_start()方法
邏輯應(yīng)該是比較清楚的
*/
function _session_start()
{
global $_SESSION_NAME, $_SESSION_TIME, $_SESSION_MEMKEY;
global $_SESSION;
global $_MEMCACHEAUTH, $_sessionmem;
$_sessionmem = memcache_connect($_MEMCACHEAUTH['host'], $_MEMCACHEAUTH['port']);
if ( empty($_COOKIE[$_SESSION_NAME]) )
{
$_SESSION_MEMKEY = md5( uniqid() );
setcookie($_SESSION_NAME, $_SESSION_MEMKEY , $_SESSION_TIME, "/");
$_SESSION = array();
}
else
{
$_SESSION_MEMKEY = $_COOKIE[$_SESSION_NAME];
$_SESSION = memcache_get($_sessionmem, $_SESSION_MEMKEY );
if ( $_SESSION === FALSE )
{
$_SESSION = array();
}
}
//注冊一個(gè)handler,這個(gè)函數(shù)會(huì)在頁面執(zhí)行完的時(shí)候執(zhí)行
register_shutdown_function("_session_save_handler");
}
/*
頁面最后執(zhí)行的方法,用于保存session值,以及關(guān)閉memcached連接
*/
function _session_save_handler()
{
global $_sessionmem;
global $_SESSION, $_SESSION_NAME, $_SESSION_EXPIRE, $_SESSION_MEMKEY;
memcache_set($_sessionmem, $_SESSION_MEMKEY, $_SESSION, 0, $_SESSION_EXPIRE);
memcache_close($_sessionmem);
}
?>


測試文件:
設(shè)置session值

復(fù)制代碼 代碼如下:


/*
設(shè)置session值文件:session_set.php
*/
include_once "memcachedsession.php";
_session_start();
$_SESSION['a'] = time();
?>


獲取session值

復(fù)制代碼 代碼如下:


/*
獲取session值文件:session_get.php
*/
include_once "memcachedsession.php";
_session_start();
function getsession()
{
echo $_SESSION['a'];
}
getsession();
?>


Memcached的緩沖應(yīng)用還是非常不錯(cuò)滴,香港空間,呵呵,,,
轉(zhuǎn)載:jincon's 包 blog
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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

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)

Hot Topics

PHP Tutorial
1502
276
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.

How to solve the problem that the Springboot2 session timeout setting is invalid How to solve the problem that the Springboot2 session timeout setting is invalid May 22, 2023 pm 01:49 PM

Problem: Today, we encountered a setting timeout problem in our project, and changes to SpringBoot2’s application.properties never took effect. Solution: The server.* properties are used to control the embedded container used by SpringBoot. SpringBoot will create an instance of the servlet container using one of the ServletWebServerFactory instances. These classes use server.* properties to configure the controlled servlet container (tomcat, jetty, etc.). When the application is deployed as a war file to a Tomcat instance, the server.* properties do not apply. They do not apply,

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

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

How to implement SMS login in Redis shared session application How to implement SMS login in Redis shared session application Jun 03, 2023 pm 03:11 PM

1. Implementing SMS login based on session 1.1 SMS login flow chart 1.2 Implementing sending SMS verification code Front-end request description: Description of request method POST request path /user/code request parameter phone (phone number) return value No back-end interface implementation: @Slf4j@ ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1. Verify mobile phone number if

How to optimize PHP application CPU usage using Memcached caching technology? How to optimize PHP application CPU usage using Memcached caching technology? Jun 21, 2023 pm 05:07 PM

With the development of the Internet, PHP applications have become more and more common in the field of Internet applications. However, high concurrent access by PHP applications can lead to high CPU usage on the server, thus affecting the performance of the application. In order to optimize the performance of PHP applications, Memcached caching technology has become a good choice. This article will introduce how to use Memcached caching technology to optimize the CPU usage of PHP applications. Introduction to Memcached caching technology Memcached is a

How PHP handles session issues in WeChat mini programs How PHP handles session issues in WeChat mini programs Jun 02, 2023 pm 03:40 PM

In recent years, WeChat mini programs have become popular all over the world and have become the platform of choice for many enterprises and individual developers. In the development of mini programs, we often encounter session problems, that is, how to save user login status in mini programs. This problem is not unfamiliar to website developers, but it is a little different in small programs. This article will introduce how to use PHP to solve session problems in WeChat mini programs. 1. Overview of the mini program login process The login process of the mini program is similar to the login process of the website and is divided into the following steps:

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.

See all articles