Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > GAH!!! Making global variables in PHP???

GAH!!! Making global variables in PHP???
Thread Tools
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 2, 2004, 09:39 PM
 
I've tried everything. I'm exhausted and frustrated from not being able to accomplish this seemingly simple task.

I'm making a simple login page for myself to use as the administrator of a site to administrate stuff. First it shows you a user field and a password field. Type your stuff in and it'll take you to the next page. This "next page" really is just a variable called $action being changed in a switch block.

Then I have an if statement to check to see if login was OK. It checks 4 things:
1) If a bloody supposed-to-be-global variable called 'auth' is set to 1
OR
2) If the $user and $pass are correct in the previously supplied form AND if $PHP_AUTH_USER is 'admin'.

[php]if ( $GLOBALS['auth'] == 1 or ( $user == "admin" and $PHP_AUTH_USER == "admin" and $pass == "booger" )) {
$GLOBALS['auth'] = 1;
....}[/php]

Yes, that's 4 things to check if counted correctly...

Now, on the first load the BOTH parts of the if statement passes so it shows me the page. INSTANTLY I set the variable "auth" to = 1. On the page I display the value of auth and it's 1. Then I proceed to clicking other stuff on the page (changing $action in the URL and loading the page again), but the variable goes back to being 0.

I know this is a bad way to create such a page, but it is private and used only by my friends so it's ok. What I don't understand is why this frickin' variable doesn't stick to 1. I've tried setting it as a $_POST['auth'] variable, as a $GLOBALL['auth'] variable, and as $_SESSION['auth']. Also as simply a variable that's declared in the very beginning of the script and therefore should be global by definition. Even as a static variable!

NOTHING WORKS!!! What am I doing wrong? I know I'm missing something very obvious here because this should be a simple thing.
(Last edited by itistoday; Jul 3, 2004 at 11:02 AM. )
     
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 2, 2004, 10:03 PM
 
Ok... I think I've figured something out. Apparently no variable can be saved like this after the page is reloaded.... So now I've got to probably resort to cookies or something painful like that...
     
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 2, 2004, 10:10 PM
 
BWUAHAHAAAAHAA!!!!!!!! *hysterical laughter*

Ok, problem solved. I just used session_start().

That was easy. See, I told it was something simple! You can ignore this thread now.
     
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Jul 3, 2004, 09:12 AM
 
I think the root of your problem is that you don't understand the statelessness of web pages. Every time you pull a page it is as if you are connecting for the first time. The sever does not remember what pages it gave you, or even that it ever saw you (or anyone else) before.

You have to layer other methods for CGI's (including PHP) to know about return visitors if you want it to remember things. I think that you are correct, and the session system is the one you want to use in this case, but I also think you need to go back and read up on the fundamentals of web development.
     
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 3, 2004, 09:49 AM
 
Originally posted by larkost:
I think the root of your problem is that you don't understand the statelessness of web pages. Every time you pull a page it is as if you are connecting for the first time. The sever does not remember what pages it gave you, or even that it ever saw you (or anyone else) before.

You have to layer other methods for CGI's (including PHP) to know about return visitors if you want it to remember things. I think that you are correct, and the session system is the one you want to use in this case, but I also think you need to go back and read up on the fundamentals of web development.
Thank you for your opinion but I think I'm managing just fine so far. I do understand this concept now that I've solved it, and before I understood it I was able to create an entire forum. I definitely don't think I need to read up on the "fundamentals" of web development—whatever that may be.
     
Occasionally Useful
Join Date: Jun 2001
Location: Liverpool, UK
Status: Offline
Reply With Quote
Jul 3, 2004, 10:07 AM
 
Originally posted by itistoday:
I definitely don't think I need to read up on the "fundamentals" of web development—whatever that may be.
i wish you knew how stupid that makes you sound. good luck, and don't expect any help from me.
"Have sharp knives. Be creative. Cook to music" ~ maxelson
     
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 3, 2004, 10:15 AM
 
Originally posted by philzilla:
i wish you knew how stupid that makes you sound. good luck, and don't expect any help from me.
that was a sarcastic statement you know? made for the simple reason of questioning what he considered "fundamentals" to be.
     
Mac Enthusiast
Join Date: Jan 1999
Location: Marietta, GA, USA
Status: Offline
Reply With Quote
Jul 6, 2004, 10:22 AM
 
Originally posted by philzilla:
i wish you knew how stupid that makes you sound. good luck, and don't expect any help from me.
Geez people, lighten up. He's learning by jumping in and trying things out, which a perfectly valid way to learn.

Ignore philzilla and larkost, they need some coffee or something. Post again and we'll help you out...
Scott Genevish
scott AT genevish DOT org
     
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 6, 2004, 02:27 PM
 
Originally posted by genevish:
Geez people, lighten up. He's learning by jumping in and trying things out, which a perfectly valid way to learn.

Ignore philzilla and larkost, they need some coffee or something. Post again and we'll help you out...
I fixed the problem but thanks for the kind words
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:39 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2