Question

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!

Was it helpful?

Solution

Ok, so. I believe the issue I'm having is I'm misunderstanding how PHP and JavaScript run. PHP runs before the page loads on the server side, and then is done running. Which means that my PHP globals are "per execution" variables. By the time the JavaScript catches the event and looks at the PHP var, it's just reading the PHP variable and the set code has already run and does not run again.

Basically, I'm confusing client versus server side scripting.

Which means there are two ways I can solve my problem: (1) use some kind of persistent storage, a.k.a a file or a database table that I check and update instead of a scripted variable, or (2) I can make an AJAX request and POST the data to the server, causing the server side code to run with the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top