开发者

Maintaining state between runs | Session Use

开发者 https://www.devze.com 2023-02-16 20:31 出处:网络
There are different way to run PHP code.For example user initiate reloads and user initiated 开发者_运维知识库ajax requests.

There are different way to run PHP code. For example user initiate reloads and user initiated 开发者_运维知识库ajax requests.

What it the best way to maintain state between these runs?


PHP does consider it separate runs. Two things:

  1. Don't use globals... they're bad :) Consider making your "session" class a collection of static functions with the session_id as a static member var.
  2. Simply create a new session class in your 2nd snippet:
$obj_ses = new session();
$obj_ses->activate('email', $this->_protected['email']);

The session id will be the same across all page views for that particular user, so creating a new session() in the second snippet will still refer to the same session you started in the first snippet.

Here's what a static implementation might look like:


// class names should be camel-cased
class SessionManager
{
    protected static $session_id = null;

    public static function start()
    {
        self::$session_id = session_start();
    }

    // ... and so on
}

// to use
SessionManager::start();
SessionManager::activate('email', $email);

That should really be all you need. There are certainly many ways to do this, but this ought to get you started :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号