...

View Full Version : Variable scope, updating global vars



eSite
03-11-2007, 05:24 AM
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 (http://fr.php.net/manual/en/language.variables.scope.php)
and hours of tweaking I cannot get the following to work.



<?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

Fou-Lu
03-11-2007, 10:17 AM
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?

eSite
03-11-2007, 11:20 AM
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.

Fou-Lu
03-11-2007, 07:47 PM
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

$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?

eSite
03-11-2007, 10:24 PM
Does this give you enough information to fix it up?

Ok, I will write it to another file.
I've browsed the Filesystem Functions (http://fr2.php.net/manual/en/ref.filesystem.php) 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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum