Table of Contents
回复内容:
Home Backend Development PHP Tutorial PHP的SESSION写入问题

PHP的SESSION写入问题

Jun 06, 2016 pm 08:36 PM
php session

是这样的,我现在打算用memcache来存储信息

这样实现的write()方法

<code>class MemcacheHandler implements \SessionHandlerInterface
{
    ...

    public function write($session_id, $session_data)
    {
        $mc = self::getMc();
        file_put_contents('/tmp/sess_log', $session_id.'-'.$session_data.'-'.self::$config['expire'].PHP_EOL, FILE_APPEND);    # 测试用
        return $mc->set($session_id, $session_data, self::$config['expire']);
    }

    ...
}
</code>
Copy after login
Copy after login

然后

<code>php</code><code>session_set_save_handler(new MemcacheHandler(), true);    #自己定义session处理

session_start();    # 开启session

$_SESSION['uid'] = 123456;    #写入数据
</code>
Copy after login
Copy after login

然后问题来了,分析/tmp/sess_log文件看出,将session数据写入memcache时,只写入了一次,当刷新页面的时候,并没有再次写入。这样的话,假设我用session来判断用户是否是登陆状态,那岂不是超过定义的时间之后,就自动掉线了?

如果我改成$_SESSION['uid'] = 78965;,这时却会再次写入。

google了下,没有找到答案,求各位大神解答一下,多谢了

回复内容:

是这样的,我现在打算用memcache来存储信息

这样实现的write()方法

<code>class MemcacheHandler implements \SessionHandlerInterface
{
    ...

    public function write($session_id, $session_data)
    {
        $mc = self::getMc();
        file_put_contents('/tmp/sess_log', $session_id.'-'.$session_data.'-'.self::$config['expire'].PHP_EOL, FILE_APPEND);    # 测试用
        return $mc->set($session_id, $session_data, self::$config['expire']);
    }

    ...
}
</code>
Copy after login
Copy after login

然后

<code>php</code><code>session_set_save_handler(new MemcacheHandler(), true);    #自己定义session处理

session_start();    # 开启session

$_SESSION['uid'] = 123456;    #写入数据
</code>
Copy after login
Copy after login

然后问题来了,分析/tmp/sess_log文件看出,将session数据写入memcache时,只写入了一次,当刷新页面的时候,并没有再次写入。这样的话,假设我用session来判断用户是否是登陆状态,那岂不是超过定义的时间之后,就自动掉线了?

如果我改成$_SESSION['uid'] = 78965;,这时却会再次写入。

google了下,没有找到答案,求各位大神解答一下,多谢了

不用自己实现memcache session handler. 你装上php memcache扩展,在php.ini里配置下就能使用memcache 存session。

session存储在memcache不是配置一下的事情么?怎么变得这么麻烦了。
php使用 memcache 来存储 session

1:改时间戳试下

http://php.net/manual/zh/memcache.set.php

2:session不推介试用memcache存储 ,具体文章自己搜下 参见: memcache 作者 session 关键字

并不是你赋值了就一定会触发写入,php应该在写入之前会判断写入被写入的值是否改变了,这也是很常见的降低写入负载的一种思路,你虽然在不停的赋值,但其实都是同一个值,所以实际session内容并没有改变

你这是PHP5.4以上的写法,我的是PHP5.2的,把session写入memcache,我的是每次都写入,已经测试了,不知道是不是版本的问题,你在write方法里在写入前打印点东西看看执行了吗?

memcache用LRU的,不推荐用它来存session

ini_set('session.save_handler', 'memcached');
或者你装了php memcache扩展
ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', $memcachePath);

$memcachePath 是一个字符串,可以拼接起多个memcached服务
看一下手册,有说明。

按照你现在的写法确实会这样,建议你在session的数据里面加个过期时间,自己续吧。

ini_set('session.save_handler', 'memcached');使用这种方式有个问题,session的GC是系统控制的,你没办法在逻辑里面控制

下面是续session时长的思路

function write(){
$md->set("xxx",array("uid"=>"xxxx","expire_time"=>自己逻辑的过期时间),memcache数据过期时间 );
}

function read(){
$data = $md->get("xxxx");
if($data){
//sesion存在
$diffTime = time() - $data["expire_time"];
if($diffTime > 阀值){
//续session的时间
}
}
}

楼主提到

当刷新页面的时候,并没有再次写入

当session没有过期的时候,session不会重新写。要看你的session配置的参数是怎么样的,是不是刷新也没不会导致过期呢,(很大可能是). 试试关掉并重新打开浏览器。

用print_r(session_get_params()) 看看结果

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles