English 中文(简体)
PHP Save Session when using session_write_close(a);
原标题:PHP Save Session when using session_write_close();
  • 时间:2012-04-06 16:54:47
  •  标签:
  • php
  • session

我在一页的开头就用了很长的投票单。

session_start();
session_write_close();

因为:

防止同时书写的文字可在每届会议上操作。

因此,如果投票时间不及投票时间长,用户将无法再装上另一页。

因此,可以从这一投票页上查阅我的届会数据是可能的,但是,在我手稿一的某个时候,由于我做了一些改动,把我的会议留给服务器。

如何做到这一点?

这将非常令人 n笑皆是,它是一种采取类似行动的方式。

session_write_open();
//do stuff
session_write_close();

但是,本届会议——公开(公开)确实存在!

成就

最佳回答

Before, 您对本届会议作了一些改动,请上session_start/code>。 作出这些改动,如果你仍然不想再提<代码>session_write_close。 你可以像你一样多次这样做。

问题回答

The previous solution will create a session ids and cookies... I wouldn t use it as is:

Session is created every time you call session_start(). If you want to avoid multiple cookie, write better code. Multiple session_start() especially for the same names in the same script seems like a really bad idea.

见:https://bugs.php.net/bug.php?id=38104

I am looking for a solution right now too and I can t find one. I agree with those who say this is a "bug". You should be able to reopen a php session, but as you said session_write_open() does not exist...

I found a workaround in the above thread. It involves sending a header specifying manually the session id s cookie after processing the request. Luckily enough I am working with a home-brewed Front Controller that works so that no sub-controller will ever send data on its own. In a nutshell, it works perfectly in my case. To use this you might just have to use ob_start() and ob_get_clean(). Here s the magic line:

if (SID) header( Set-Cookie:  .SID. ; path=/ , true);

EDIT : see CMCDragonkai s answer below, seems good!?

All of the answers here seem to be saying to use the session methods in ways that they were clearly not intended to be used...namely calling session_start() more than once.

PHP网站提供了一个榜样,即“Handler Interface”执行,将像现有会议一样,但不会锁定档案。 仅仅落实它们的榜样,就把我lock锁的问题固定下来,以便在同届会议上同时建立联系,同时又不限制我增加届会次数的能力。 为了防止某些种族条件,由于本届会议没有完全的无国籍状态,我确实必须设法在未结束的情形下挽救会议,以便重大变革能够在变革之后立即拯救出来,而不太重要的几届会议只能在请求结束时挽救。 见以下使用实例:

Session::start();
echo("<pre>Vars Stored in Session Were:
");print_r($_SESSION);echo("</pre>");

$_SESSION[ one ]    =  one ;
$_SESSION[ two ]    =  two ;
//save won t close session and subsequent request will show  three 
Session::save(); 
$_SESSION[ three ]  =  three ;

如果您取代<代码>Session:start(> with session_start() and :save( with session_write_close(),请您通知,以后的请求永远不会打印出第三个变数......否则将丢失。 然而,使用Handler会议(以下),没有数据。

执行《行动计划》需要PHP5.4+。 但是,你可以在旧版本的PHP上提供个人反馈方法:。 见

namespace {
    class Session implements SessionHandlerInterface {
        /** @var Session */
        private static $_instance;
        private $savePath;

        public static function start() {
            if( empty(self::$_instance) ) {
                self::$_instance = new self();
                session_set_save_handler(self::$_instance,true);
                session_start();
            }
        }
        public static function save() {
            if( empty(self::$_instance) ) {
                throw new Exception("You cannot save a session before starting the session");
            }
            self::$_instance->write(session_id(),session_encode());
        }
        public function open($savePath, $sessionName) {
            $this->savePath = $savePath;
            if (!is_dir($this->savePath)) {
                mkdir($this->savePath, 0777);
            }

            return true;
        }
        public function close() {
            return true;
        }
        public function read($id) {
            return (string)@file_get_contents("$this->savePath/sess_$id");
        }
        public function write($id, $data) {
            return file_put_contents("$this->savePath/sess_$id", $data) === false ? false : true;
        }
        public function destroy($id) {
            $file = "$this->savePath/sess_$id";
            if (file_exists($file)) {
                unlink($file);
            }

            return true;
        }
        public function gc($maxlifetime) {
            foreach (glob("$this->savePath/sess_*") as $file) {
                if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
                    unlink($file);
                }
            }

            return true;
        }
    }

这里的其他答案是好的解决方案。 正如“Jon”所提到,在你想作出改变之前,这套骗子是再次叫会议——启动。 然后,当你做些改动时,再次打电话到会。

正如“Armel Larcier”所指出的,问题在于PHP试图产生新的头盔,并有可能产生警告(例如,如果你已经向客户提供非头盔数据)。 当然,你可以简单地把本届会议定为“@”(......session_start()),但有一个更好的办法。

Another Stack Overflow question, provided by @VolkerK reveals the best answer:

session_start(); // first session_start
...
session_write_close();
...

ini_set( session.use_only_cookies , false);
ini_set( session.use_cookies , false);
//ini_set( session.use_trans_sid , false); //May be necessary in some situations
ini_set( session.cache_limiter , null);
session_start(); // second session_start

This prevents PHP from attempting to send the headers again. You could even write a helper function to wrap the ini_set() functions to make this a bit more convenient:

function session_reopen() {
    ini_set( session.use_only_cookies , false);
    ini_set( session.use_cookies , false);
    //ini_set( session.use_trans_sid , false); //May be necessary in some situations
    ini_set( session.cache_limiter , null);
    session_start(); //Reopen the (previously closed) session for writing.
}

最初相关的SO问题/回答:

在对Armel Larcier进行测试后,他们的工作围绕面进行。 我在此提议解决这一问题:

    ob_start();

    session_start();
    session_write_close();

    session_start();
    session_write_close();

    session_start();
    session_write_close();

    session_start();
    session_write_close();

    if(SID){

        $headers =  array_unique(headers_list());   

        $cookie_strings = array();

        foreach($headers as $header){
            if(preg_match( /^Set-Cookie: (.+)/ , $header, $matches)){
                $cookie_strings[] = $matches[1];
            }
        }

        header_remove( Set-Cookie );

        foreach($cookie_strings as $cookie){
            header( Set-Cookie:   . $cookie, false);
        }

    }

    ob_flush();

这将保留在与届会之前建立的任何厨师。

BTW, 您不妨将上述代码登记为登记_shutdown_Function。 保证在职能之前开始使用斜线,并在职能范围内执行 ob。





相关问题
why the session in iis automatically log out?

I used iis6, and when i called a function Directory.delete(), the all the session relate this website will be logged out. and i debugged the website, i found no exception. any one have ideas on this ? ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Can I get the size of a Session object in bytes in c#?

Is it possible to get the size(in bytes) of a Session object after storing something such as a datatable inside it? I want to get the size of a particular Session object, such as Session["table1"], ...

提供严格分类的出席会议物体

提供严格分类的与会机会的最佳方式是什么? 我正计划转而选择矩阵,这正在促使汇编者抱怨我的幻觉方案拟订方法......

PHP Session is not destroying after user logout

I m trying to create an authentication mechanism for my PHP Application and I m having difficulty destroying the session. I ve tried unsetting the authentication token which was previously set within ...

热门标签