dongrouyuan5685 2011-06-23 12:39
浏览 41
已采纳

PHP Singleton对象消失了吗?

I am stuck in a frustrating rut here. I have an authentication system (built from scratch) that makes use of a singleton object.

The singleton object basically manages the security of sessions and has functions that safeguard against session hijacking and other malicious activities.

These functions depend on member data.

Now the issue is that PHP seems to discard these singleton objects every time the user refreshes or moves to a new page.

Here is a prototype of the sessions class:

class session extends login{
    public   $sessionid;
    private  $fingerprint;
    public  static $temp=0;
    public  static $s_instance = NULL;

    public static function s_getinstance(){

        if (!isset(session::$s_instance) || !isset(session::$sessionid)) {
           $c = __CLASS__;
           if(isset(session::$s_instance)) {
               session::$s_instance = 0;
           }

           session::$s_instance = new $c;
           self::regenerate_id_name();                    
           self::$temp +=1;                
        }

        return session::$s_instance;
    }
}

The last function checks the data member ($s_insntance), if it is NULL it creates an object and throws it back along with managing the activities related to creating a new session.

If the data member is not null, it returns the existing object.

Or in theory, that is what it is supposed to do. However, every time I visit a new page and call upon the s_getinstance function, it tries creating a brand new object for some reason and the old data is lost. Please help me out here.

  • 写回答

1条回答 默认 最新

  • donglanche9257 2011-06-23 12:45
    关注

    What we don't see here is at any point you save the contents of your session object into the $_SESSION. Without doing so, it cannot persist across a page load.

    You need a method to save the session instance into the PHP $_SESSION and then your s_getinstance() needs to check if already exists in $_SESSION and retrieve it from there, or retrieve it from memory, or create it from scratch if it doesn't exist anywhere.

    Start reading here... (Docs on PHP session handling)

    // assuming you've already called session_start()...
    public function storeinstance()
    {
      $_SESSION['session'] = self::s_getinstance();
    }
    
    public static function s_getinstance(){
    
        if (!isset(session::$s_instance) || !isset(session::$sessionid)) {
           $c = __CLASS__;
    
           // Check if it's already sitting in $_SESSION
           // Load it from $_SESSION if it's there, and then unset the $_SESSION var
           if (!isset(session::$s_instance) && isset($_SESSION['session'])) {
               session::$s_instance = $_SESSION['session'];
               unset($_SESSION['session']);
           }
           else if(isset(session::$s_instance)) {
               session::$s_instance = 0;
           }
    
           session::$s_instance = new $c;
           self::regenerate_id_name();                    
           self::$temp +=1;                
        }
    
        return session::$s_instance;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装