<?php
/**
* Watches for changes in authentication tokens for a local utility.
* This code does NOT use async logic.
*/
// Configuration
$tokenFilePath = 'token.txt'; // Path to the file storing the token.
$watchInterval = 5; // Interval in seconds to check for changes.
// Function to read the token from the file.
function getToken() {
if (file_exists($tokenFilePath)) {
return file_get_contents($tokenFilePath);
}
return null;
}
// Function to write the token to the file.
function setToken($token) {
file_put_contents($tokenFilePath, $token);
}
// Initial token retrieval
$currentToken = getToken();
// Main loop
while (true) {
$newToken = getToken();
if ($newToken !== $currentToken) {
// Token has changed!
echo "Token changed: " . $newToken . "\n";
// Update the current token
$currentToken = $newToken;
// You can add your utility logic here that uses the new token.
// For example, re-authenticate, update session variables, etc.
//Example: set a session variable.
session_start();
$_SESSION['token'] = $newToken;
}
// Wait for the specified interval
sleep($watchInterval);
}
?>
Add your comment