1. <?php
  2. // Define a flag for dry-run mode
  3. session_start();
  4. $dryRun = isset($_GET['dryRun']) && $_GET['dryRun'] === 'true';
  5. // Function to replace session cookie values with fallback logic
  6. function replaceSessionValues($data) {
  7. global $dryRun;
  8. if ($dryRun) {
  9. // Dry-run: Log the changes instead of applying them.
  10. error_log("Dry-run: Would set session values: " . print_r($data, true));
  11. return; // Don't actually set the cookies. Important!
  12. }
  13. // Apply session value changes
  14. foreach ($data as $key => $value) {
  15. $_SESSION[$key] = $value;
  16. }
  17. }
  18. // Example usage (replace with your actual logic)
  19. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  20. // Simulate data to be set in session
  21. $sessionData = [
  22. 'username' => 'testuser',
  23. 'user_id' => 123,
  24. 'last_login' => date('Y-m-d H:i:s')
  25. ];
  26. // Replace session values with fallback logic
  27. replaceSessionValues($sessionData);
  28. //Example setting a value with fallback
  29. $_SESSION['fallback_value'] = 'default_value'; // This will set the value even in dry-run mode.
  30. echo "Session values updated (or would be updated in dry-run).";
  31. }
  32. ?>

Add your comment