Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Variable scope, updating global vars

    Hello,

    I make 2 different HTTP requests to a web page (examples) :
    ?update_sender_key=qg5DDG
    ?req_type=update&check_for=redcar&asker_key=jilo987BZDL


    After reading the php.net doc about variables.scope
    and hours of tweaking I cannot get the following to work.

    PHP Code:
    <?php

    $update_sender_key_var 
    "abc";

    if (
    $_GET["update_sender_key"] != $update_sender_key_var && $_GET["update_sender_key"] != "")
    {
    global 
    $update_sender_key_var;
    $update_sender_key_var $_GET["update_sender_key"];
    $return $update_sender_key_var;
    echo 
    $return;
    }

    if (
    $_GET["req_type"] == "update")
    {
    global 
    $update_sender_key_var;
    echo 
    $update_sender_key_var;
    mail($update_sender_key_var."@lsl.secondlife.com""update_check"$_GET["asker_key"]."@".$_GET["check_for"]);
    }

    ?>
    Both HTTP requests are made on irregular basis.
    In the second if statement, echo $update_sender_key_var; should return qg5DDG, but it returns abc

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Oops your scope is out. The idea is correct, but the implementation is incorrect.
    Scopes refer more often to method/function scopes.

    If statements are not considered to contain their own level of visibility. Therefore global is not required on your if statements.

    As with your code, I'm completely confused by what it is actually supposed to do. Can you be a little more specific as to what it does? Does it take all sets of your defined parameters? One or the other? What about none? Can you be a little more specific as to the expect value range, whether it requires specific values or if it can be variable for example?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Can you be a little more specific as to what it does?
    That PHP script is a relay between an updater and an old version (to update).
    If the updater gets a new UUID, it will tell the script with this request :
    ?update_sender_key=qg5DDG
    the script will then store the new UUID.

    When the old version checks for a new version, it sends this request :
    ?req_type=update&check_for=redcar&asker_key=jilo987BZDL
    and the scripts emails the updater, using the previously set UUID.


    Does it take all sets of your defined parameters? One or the other? What about none?
    Not sure what you mean.
    The 1st if statement corresponds to the 1st HTTP request, the 2nd to the 2nd HTTP request.
    All parameters of a request are necessary.

    Can you be a little more specific as to the expect value range, whether it requires specific values or if it can be variable for example?
    Some values such as "update" are predefined, others such as (keys) UUID are random. But I don't see how it matters here.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Ok I got you, and you answered what I was looking for.

    The problem is, $update_sender_key_var will never really change. Since it is embedded in the script work, even when youi send the request ?update_sender_key, the value on the next fetch still retains the coded value. The best solution is to use a database or external file of some sorts to store the value of $update_sender_key, otherwise you will need to overwrite the current variable.
    Here's a sort of visual example for you to use:
    PHP Code:
    <?php

    $var1 
    1;

    if (isset(
    $_REQUEST['updateVar']) && !empty($_REQUEST['updateVar']))
    {
        
    // $var1 is set to the new value of updateVar
        
    $var1 = (int)$_REQUEST['updateVar'];
    }

    if (isset(
    $_REQUEST['fetchVar']))
    {
        
    // print the value of $var1
        
    echo $var1;
    }
    ?>
    If the querystring is like so:
    ?updateVar=5&fetchVar
    The output will be:
    5
    If the request is
    ?updateVar=5
    This sets $var1 to 5
    and then ?fetchVar is called, the output will be
    1
    This is simply because the script reads the initial value, the scope is fine however the data is not changed (it is immutable). So, your best option would be to use a database to control the value of your new key.

    Does this give you enough information to fix it up?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Quote Originally Posted by Fou-Lu View Post
    Does this give you enough information to fix it up?
    Ok, I will write it to another file.
    I've browsed the Filesystem Functions but cannot find how to clear the content of the file or replace the value of a variable.

    I'm thinking of writing $update_sender_key_var = $_GET["update_sender_key"]; which will result in writing $update_sender_key_var = "qg5DDG"; then include that file.
    I think it's how web applications deal with their configuration file.
    Last edited by eSite; 03-11-2007 at 11:28 PM. Reason: typo


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •