Forum Moderators: coopster
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]
<?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"]);
}
?>
?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.
Good luck! :)
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.
[edited by: eSite at 6:42 pm (utc) on Mar. 11, 2007]
Oh. Then a flatfile or database would be what you'd have to use :)
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]
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.
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.
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'.