2015-12-17 71 views
1

我正在編寫一個PHP中的大腳本,它需要從未來用戶創建的擴展中安全。 我想通了,要創造這樣的權限類檢查,以一些核心變量等擴展訪問從類中創建類的新實例它在PHP中擴展

這裏是啓動類:

class SystemStartup 
{ 
    private function startClass () { 
     require (dirname(__FILE__) . '/sys.run.php'); 
     //starting SystemRun with something like core security token 
     new SystemRun (hash ('sha256' , $_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR'] . microtime (TRUE))); 
    } 
    function __construct () { 
     //here i have some security stuff like checking class not already running 
     $this -> startClass (); 
    } 
    function __destruct () { 
     //wywolane na samym koncu 
    } 

}; 

SystemRun類:

class SystemRun 
{ 
    private  $SystemToken; 

    //static because i want to be callable from other classes extending SystemRun 
    protected static $SystemUrlControl; 

    private function startClass () { 

     //before more code 

     //loading file with child class 
     require (dirname(__FILE__) . '/urlcontrol.php'); 
     //starting it with security token 
     echo $this -> SystemToken; //just for check 
     self :: $SystemUrlControl = new SystemUrlControl ($this -> SystemToken); 

     //more more more code (its main class) 
    } 
    protected function validateToken ($SystemToken) { 
     echo $this -> SystemToken; //just for check 
     //this should check tokens are the same 
     if ($this -> SystemToken == $SystemToken) return TRUE; 
     return FALSE; 
    } 
    function __construct ($SystemToken) { 
     //saving token for next classes 
     $this -> SystemToken = $SystemToken; 
     $this -> startClass (); 
    } 
}; 

現在SystemUrlControl與擴展SystemRun

class SystemUrlControl extends SystemRun 
{ 
    private   $SystemToken; 

    //functions 

    //more functions 

    //more more more 

    function __construct ($SystemToken) { 
     if (!parent :: validateToken ($SystemToken)) echo 'somebody else called me!'; 
     $this -> SystemToken = $SystemToken; 
    } 
}; 

問題是從SystemUrlControl調用validateToken返回FALSE - SystemRun將它自己的SystemToken視爲NULL。

輸出(從回聲)是:

> 5b6a09d8f03dd7e6229a5... (valid token) (here NULL value) somebody else called me! 
+0

'自:: $ SystemToken'不是一個靜態的成員,因此必須用'$這個 - > SystemToken'使用。 – Ian

+0

其中一個「ctrl + z」小於調試時會改變;) –

回答

1

在你validateToken方法你有這個。

$this -> $SystemToken 

應該

$this -> SystemToken 
+0

這個錯誤很可能是由奇怪的格式造成的($ this->'和'self ::'和成員變量/函數之間有空格) – Ian

+0

是,在這裏寫代碼時它的錯誤:)在文件中我有$ this - > SystemToken。謝謝 :) –