1

In my PHP project I created a singletone class that mange my access tokens.
The tokens are some constant strings with timestamp of +/- 5 min hashed with SHA1.
I want that each page can access to this singletone instance (the same instance) because it holds the tokens. Also there going to be a procedure that refresh the tokens in that instance.
The name TokenManager with located in different php file.
I create the instance $TOKENS in different file.

<?php require_once 'TokenManager.php'; $TOKENS = TokenManager::Instance(); 

And another file for refresh action (refreshTokens.php):

<?php require_once 'Tokens.php'; global $TOKENS; $TOKENS->refreshTokens(); var_dump($TOKENS->tokens); 

In another page that is a web service (AddUser) I use the this Tokens.php instance as global.

 require_once 'TokenManager.php'; require_once 'Tokens.php'; ................................... function start(){ global $userParams; global $TOKENS; //Check for all mandatory params if(!ValidateParams()){ finish(); } if(!$TOKENS->checkTokenAndGetChannel($userParams[PARAM_TOKEN])){ setError(ERR6_BAD_TOKEN, CODE6_DESC); finish(); } if(!isEmailValidByDrupal($userParams[PARAM_EMAIL])){ setError(ERR3_BAD_EMAIL, CODE3_DESC . $userParams[PARAM_EMAIL]); finish(); } finish(); } 

The problem that each time I call refreshTokens.php and take the token I have each time new instance with a different values what makes the tokens each time invalid.

What can I do about that?

1 Answer 1

1

Well when a called PHP Script ends all objects are destroyed and a singleton class will not really help you here.

A singleton class can be used as a cache or storage during the entire runtime of one page-call.

In other words, the singleton instance alone cannot keep the data between script calls.

Save them in the session, in a file or a database.

@Edit: After discussing the issue we came up it is the best to use class constants, I present you an example here:

@Edit2: Rethought the discussion and a Singleton class is fine if you do it correct, I leave you an example here:

final class Tokens { static private $instance = null; private $tokens = array(); static public function getInstance() { if(self::$instance === null) { self::$instance = new self(); self::$instance->initialize(); } return self::$instance; } private function initialize() { $this->tokens[] = "erhdfhegrtoken1!"; $this->tokens[] = "4h43gherhtoken2!"; $this->tokens[] = "egegtoken3!"; } public function getToken($index) { $retVal = ""; if(isset($this->tokens[$index])) { $retVal = $this->tokens[$index]; } return $retVal; } private function __construct() { } private function __clone() { } } 

Usage:

$tokens = Tokens::getInstance(); $myToken = $tokens->getToken(2); 
Sign up to request clarification or add additional context in comments.

8 Comments

If I save it in Session is it going to be the save instance for different users?
Why should it be the same for different users if it's saved in session? Each user has their own unique session!
No as Mark said, this will be fine the only problem is that you save it redundant and you should think about if you really want that I think a database table is the better option. This is also flexible for the future if you want to change your tokens or whatever comes next...
I the tokens set are the same for all user in current time interval, I don't want to read each time from file or database. I need that this object will be single instance and global for all pages and users.
@NickF Well in this case you should use constants in the class, I will edit my answer with an example, give me 2 minutes...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.