1. <?php
  2. /**
  3. * Watches for changes in authentication tokens for a local utility.
  4. * This code does NOT use async logic.
  5. */
  6. // Configuration
  7. $tokenFilePath = 'token.txt'; // Path to the file storing the token.
  8. $watchInterval = 5; // Interval in seconds to check for changes.
  9. // Function to read the token from the file.
  10. function getToken() {
  11. if (file_exists($tokenFilePath)) {
  12. return file_get_contents($tokenFilePath);
  13. }
  14. return null;
  15. }
  16. // Function to write the token to the file.
  17. function setToken($token) {
  18. file_put_contents($tokenFilePath, $token);
  19. }
  20. // Initial token retrieval
  21. $currentToken = getToken();
  22. // Main loop
  23. while (true) {
  24. $newToken = getToken();
  25. if ($newToken !== $currentToken) {
  26. // Token has changed!
  27. echo "Token changed: " . $newToken . "\n";
  28. // Update the current token
  29. $currentToken = $newToken;
  30. // You can add your utility logic here that uses the new token.
  31. // For example, re-authenticate, update session variables, etc.
  32. //Example: set a session variable.
  33. session_start();
  34. $_SESSION['token'] = $newToken;
  35. }
  36. // Wait for the specified interval
  37. sleep($watchInterval);
  38. }
  39. ?>

Add your comment