Forum Moderators: coopster

Message Too Old, No Replies

Variable scope, updating global vars

         

eSite

3:04 am on Mar 11, 2007 (gmt 0)



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 [fr.php.net]
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."@example.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

[edited by: eelixduppy at 3:13 am (utc) on Mar. 11, 2007]
[edit reason] exemplified domain name [/edit]

cmarshall

4:03 am on Mar 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try This:


<?php

[red][b]global $update_sender_key_var;[/b][/red]

$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."@example.com", "update_check", $_GET["asker_key"]."@".$_GET["check_for"]);
}

?>

eSite

4:33 am on Mar 11, 2007 (gmt 0)



Same result :(

cmarshall

5:08 am on Mar 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



?update_sender_key=qg5DDG
?req_type=update&check_for=redcar&asker_key=jilo987BZDL

Wait a minute. You are making these at different times!

<smacks forehead />

The previous sender key update will be lost. You need to keep it in a persistent variable like a database, session or a cookie.

The file will be at a pristine state whenever it is invoked. Global is only global for the single connection; not across connections.

eSite

10:26 am on Mar 11, 2007 (gmt 0)



Ah, crap.
Database sounds difficult, cookie impossible since it's not a browser, session i don't know, so I'll try writing the value in another file!

eelixduppy

3:24 pm on Mar 11, 2007 (gmt 0)



Sessions are actually very easy to use if you are willing to give it a shot. Check php.net for some specific examples as well as the list of Session Handling Functions [us3.php.net].

Good luck! :)

cmarshall

3:52 pm on Mar 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



so I'll try writing the value in another file

Don't do that. All kinds of nasty things can happen from collisions.

eelixduppy gave some very good advice. Sessions are your best bet.

A session only lasts for the amount of time your user keeps their browser open. Even if they navigate away from your site and come back, the session will still be valid, as long as they didn't close their browser. The latest Mozilla browsers have a way to resume sessions, but I haven't played with it much.

They are damn easy to use. Not much more difficult than using globals.

eSite

6:41 pm on Mar 11, 2007 (gmt 0)



So I don't think I can use sessions either since they require a browser, plus I need the data to be stored permanently.

[edited by: eSite at 6:42 pm (utc) on Mar. 11, 2007]

eelixduppy

6:43 pm on Mar 11, 2007 (gmt 0)



>> need the data to be stored permanently

Oh. Then a flatfile or database would be what you'd have to use :)

eSite

9:34 pm on Mar 11, 2007 (gmt 0)



Ok, I will write it to another file.
I've browsed the Filesystem Functions [fr2.php.net] 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.

[edited by: eSite at 9:36 pm (utc) on Mar. 11, 2007]

cmarshall

9:49 pm on Mar 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just open the file with w access. It will wipe it. You can also set eof to 0.

Now, you do realize that writing a file will explode -badly- if more than one visitor is hitting the site, right?

If you will only ever have one visitor that visits the site, and you can ABSOLUTELY GUARANTEE that you will never have an instance of more than one visitor hitting the site, then this is a very ugly possibility. Otherwise, you are going to get some real good experience debugging PHP.

Also, if you only use one file, User A will set the preferences for Users B and C. And vice-versa.

If you want to keep user preferences, then you are better off using cookies.

eSite

12:58 am on Mar 12, 2007 (gmt 0)



Just open the file with w access. It will wipe it. You can also set eof to 0.

Thanks!


Now, you do realize that writing a file will explode -badly- if more than one visitor is hitting the site, right?

If you will only ever have one visitor that visits the site, and you can ABSOLUTELY GUARANTEE that you will never have an instance of more than one visitor hitting the site, then this is a very ugly possibility. Otherwise, you are going to get some real good experience debugging PHP.


I don't understand.


Also, if you only use one file, User A will set the preferences for Users B and C. And vice-versa.

A unique "user" is making the request?update_sender_key=qg5DDG

cmarshall

1:36 am on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, the basic deal is that a Web site is a chaotic, reactive environment.

A Web server is designed to have multiple connections going simultaneously. A busy site, like Google, may have a million connections going on at once.

The server gives each connection its own copy of the server file. Only one file is updated, but each connection feels as if they each have complete control of it.

If you have a file, or a database entry that is unique, then every single connection is trying to modify it at once.

Bad news.

Cookies allow each connection to have its own state, and the server handles the match between the cookie data and your server file. If you choose a persistent cookie, then each user carries their own little "goodie bag," and brings it back with them the next time they visit. This way, one user can have state A, and another user can have state B. Your file doesn't need to keep track of it, because each user tells it what their state is, and you can even have multiple users with multiple states simultaneously accessing the server.

I know that you are worried about complexity and difficulty, but what you are trying to do is something that PHP has mechanisms to handle. If you don't learn to use these mechanisms (cookies and sessions), then you are in for a world of hurtin'.