Variable scope, updating global vars

They have: 11 posts

Joined: Sep 2004

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

They have: 27 posts

Joined: Mar 2007

Hello,

I'm not a PHP expert, but I think the reason the second IF statement is returning 'abc' is because you set it line on line 1 and in the second example the first IF statement is false. Since there is no update_sender_key in the URL, it's inheriting the value from the first line.

This may just be giving you are problem in testing because using that 'abc' value. I'm guessing that 'abc' will be replaced with a real variable, in which case your code would probably work the way you are expecting it to. The problem is that you are resetting the $update_sender_key to equal 'abc' when the page loads, so any previously set value, would be replaced with the first line.

It's hard to tell without the rest of the code, but that's my guess. I hope that helps (and makes sense). Wink

They have: 11 posts

Joined: Sep 2004

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.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.