Search code examples
phpglobal-variablesdynamic-variables

How to set and retrieve dynamic global variables in PHP without functions


I am trying to write a simple page that will save some data to the GLOBAL state (stored in server memory) using a dynamic variable name. In simple terms, there are only two parameters that can be sent by the query string (GET only).

  • jam: the parameter identifying a unique piece of data, which can be virtually any text string (including numbers). This value is appended to the string "jam" to create the dynamic variable name. So ?jam=123 results in global variable jam123. Likewise So ?jam=Booboo results in global variable jamBooboo.

  • section: the value to which the global variable mentioned above will be set (must be numeric). So if ?jam=Booboo&section=4 is passed, the value of global variable jamBooboo is set to 4.

The idea is that once the values are set for each dynamically named jam (jamXXXX), any future requests specifying that same jam should output the set value. For instance, and requests specifying ?jam=Booboo AFTER the page is called with parameters ?jam=Booboo&section=4, should simply output "4". But if the next request is ?jam=Booboo&section=6, then subsequent requests should output "6".

This is set up on PHP7 on the AWS Lightsail LAMP Stack. I am open to using another variable scope if it will persist in Server memory (not a cookie).

I have tried the code below, but while I see the initial value properly when the global variable is SET, I do not see it output on subsequent requests. I am not sure if there is a missing setting in PHP, or if my code is off somehow. I have also tried using the global keyword and looking for a register_globals setting in php.ini (doesn't exist currently).

<?php
    if(isset($_GET['jam']) == false){
        // Do nothing, it's a bad request - jam is a required variable
    }
    else if (isset($_GET['section'])  == false){ 
        // section is not specified, so output the global value of jam if it exists
        if(isset($GLOBALS["jam".$_GET['jam']])){
            echo $GLOBALS["jam".$_GET['jam']];
        }
    }
    else if (is_numeric($_GET['section'])){
        // section is specified and its a number so set the global value for jam to that number and output it
        $GLOBALS["jam".$_GET['jam']] = $_GET['section'];
        echo $GLOBALS["jam".$_GET['jam']];
    }
    else{
        // Do nothing, it's a bad request as none of the mandatory conditions have been met
    }
?>

Solution

  • If I'm not mistaken, you seem to expect $GLOBALS to persist between 2 different PHP request.

    Unless otherwise programmed, each PHP call would recieve its own memory space that DO NOT share with each another. So a variable saved to the $GLOBALS array will only be available to that request, and will be forgotten immediately when that request end. Subsequent call to the PHP code on the same server (even the same script file) do not received the $GLOBALS values from previous calls.

    You need to program you persistent storage with files, cache service or databases.