I'm trying to create a global "ServerStatus" flag for a CakePHP application. The idea is I can toggle an offline/online mode for maintenance so that when data is being pushed to the server, the site is offline.
I have a JavaScript toggle set up, as well as the redirect, but the issue I'm running into is in the global variable itself.
I've tried two things, both of them in the bootstrap.php file:
(1) I used the Configure Class to write the initial status, and
(2) I used the $GLOBALS array to write the initial value.
When I try and, later on, change the status to the alternate (non-initial) value later on when the toggle is activated, it fails to change. As far as I can tell, this means that when I write a global variable in bootstrap.php, it acts like a constant and that initial value overrides changes later on.
The toggle is a small script in my default Layout default.ctp:
<script type="text/javascript">
var charInputs = [];
setInterval(clearInputBuffer(), 20000);
function keyPressHandler(event)
{
var currChar = String.fromCharCode(event.charCode);
charInputs.push(currChar);
//alert(currChar + ' key pressed!');
checkForKeyPhrases(charInputs);
}
function checkForKeyPhrases(chars)
{
// Get inputted chars as a sequence
var seq = "";
for (var i=0; i < chars.length; i++)
seq += chars[i] + "";
// Check for "Maintenance Mode"
if (seq == ":maintenance")
{
alert("Maintenance mode... go!");
<?php
if ($GLOBALS['ServerOnline'])
{
$GLOBALS['status'] = 'down';
$GLOBALS['ServerOnline'] = false;
}
else
{
$GLOBALS['status'] = 'up';
$GLOBALS['ServerOnline'] = true;
}
?>
var temp = '<?php echo $GLOBALS['status']; ?>';
alert('Current Server Status: ' + temp);
clearInputBuffer();
}
else if (seq.indexOf('clear') > -1)
clearInputBuffer();
}
function clearInputBuffer()
{ charInputs = []; }
</script>
In bootstrap.php, I write $GLOBALS['ServerOnline'] = true;
; but when the alert pops up, it always registers the initial value of true and I see "down" as the current status. It never changes over.
If I'm correct in interpreting this to mean that writing a global in the initialization bootstrap file overrides later changes, where would be the best place to write a dynamic GLOBAL's initial value for a CakePHP app?
If I'm wrong, does anyone see what I'm missing?
Thanks for your help ahead of time!