<?php
// Define a flag for dry-run mode
session_start();
$dryRun = isset($_GET['dryRun']) && $_GET['dryRun'] === 'true';
// Function to replace session cookie values with fallback logic
function replaceSessionValues($data) {
global $dryRun;
if ($dryRun) {
// Dry-run: Log the changes instead of applying them.
error_log("Dry-run: Would set session values: " . print_r($data, true));
return; // Don't actually set the cookies. Important!
}
// Apply session value changes
foreach ($data as $key => $value) {
$_SESSION[$key] = $value;
}
}
// Example usage (replace with your actual logic)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Simulate data to be set in session
$sessionData = [
'username' => 'testuser',
'user_id' => 123,
'last_login' => date('Y-m-d H:i:s')
];
// Replace session values with fallback logic
replaceSessionValues($sessionData);
//Example setting a value with fallback
$_SESSION['fallback_value'] = 'default_value'; // This will set the value even in dry-run mode.
echo "Session values updated (or would be updated in dry-run).";
}
?>
Add your comment