So I got this api that returns a token bearer with also the expiration which is actually 5 minutes (300 seconds).
So I've started storing it into a session variable after session start:
function check_token_status() {
if (empty($_SESSION['token_bearer'])) {
$echo = "new token added.";
$_SESSION['token_bearer']=get_new_token(); //function that retrieves the token.
} else {
$echo = "reusing token.";
}
So, that function is completely useless because the $_SESSION['token_bearer'] will be always empty on a new session. So I first need to fulfill the $_SESSION['token_bearer'] session value with the value returned by the get_new_token. The thing is that i've tried to extract the value from expires_in key which is 300 seconds (5 mins) and somehow count, if the current time is beyond the expires_in then renew the token.
function check_token_status() {
if (empty($_SESSION['token_bearer'])) {
$echo = "new token added."; //first time run
$_SESSION['token_bearer']=get_new_token(); //function that retrieves the token.
} else { //the token is still available to use.
if (isset($_SESSION['start']) && (time() - $_SESSION['start'] > $_SESSION['expires_in'])) {
$_SESSION['token_bearer']=get_new_token();
echo "token time exceeded, requesting a new token.";
} else {
$_SESSION['token_bearer']=$_SESSION['token_bearer'];
}
}
}
Since this is required for an external api and is not related with the current session login in, it should renew each time it exceeds the 5 min and if not reuse the current value of the token. But somehow is always trying to renew the token instead of using the current on which has until 5 minutes to reuse it.
Any tip will be appreciated.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.