Page 2 of 3
Posted: Tue Feb 28, 2006 8:00 pm
by JellyFish
Thank you all. Wow I didn't realize how much attention my post got and I thank you all for that.
The fact of the matter is I'm vary new at php and this information is really, really useful. I'm assuming that what you mean when you say globals are evil or bad, is that, if I try and use a variable name in another script, not knowing that I used this name as a global before, that the value of that name is still specified my a previuos script that uses the name as a global.
Thanks agian for all these post. Just checking if I got this right, when I want to asign a value to my global variable I:
Code: Select all
$GLOBAL['error_msg'] = "Username or Password is Incorrect.";
And when I want to display it I ether:
Or:
Most likely I'd use the $GLOBAL to echo the contents instade of $error_msg but I'm not quite sure.
Thanks.
Posted: Tue Feb 28, 2006 8:03 pm
by josh
not
Using quotes where they are not needed is really messing you up
----
@ Roja, I was wrong.. thanks for putting me in my place

Posted: Tue Feb 28, 2006 8:08 pm
by Benjamin
Well I figured I would ruffle some feathers with that comment about a Session being a global. I understand what Roja is saying about how Superglobals have different properties than regular globals in that you cannot unset them etc. All I am saying is that even though it may have different properties, it's still a global variable, perhaps even more so then a regular global. Sorry if I made you upset Roja.
Posted: Tue Feb 28, 2006 8:32 pm
by JellyFish
Thank you jshpro2. I don't usually put quotes when echoing a var or something like that. Thanks for answering my question and thanks for correcting me on that.

Posted: Tue Feb 28, 2006 8:43 pm
by JellyFish
Well apparently I'm doing something wrong. I am setting the $GLOBAL['error_msg'] like so.
Code: Select all
$GLOBAL['error_msg'] = "Incorrect Username or Password.";
header("Location: show_login.php");
exit;
And I am trying to display like this.
And Where I'm trying to display it and where I'm trying to set the var are in to seperate places and I don't think that that is a problem considering the fact that that is what globals are for.
Posted: Tue Feb 28, 2006 8:51 pm
by Benjamin
EasyGoing wrote:Well apparently I'm doing something wrong. I am setting the $GLOBAL['error_msg'] like so.
Global variables won't be transferred to another script. You may want to put it in a Session.
Posted: Tue Feb 28, 2006 8:51 pm
by John Cartwright
once again, why are you using globals?? If need be stick it in a session.
psuedo:
Code: Select all
session_start();
... page stuff ?
if (IS_AN_ERROR) {
$_SESSION['error_message'] = "Incorrect Username or Password.";
}
Debugging globals is just too much of a mess,
hear my warning.
p.s. its
$GLOBAL`S`
agtlewis wrote:EasyGoing wrote:Well apparently I'm doing something wrong. I am setting the $GLOBAL['error_msg'] like so.
Global variables won't be transferred to another script. You may want to put it in a Session.
What do you mean? Of course they will as long as the script has been included at some point or another during execution.
Re: Global vars
Posted: Tue Feb 28, 2006 10:27 pm
by Christopher
EasyGoing wrote:For some reason it doesn't work cause I assume that the variable isn't global. All I need to know is how to make a variable global.
OK, I believe that you now know that you use the syntax "global $myvar;" inside a function to make a variable in the global namespace visible within the function.
But hopefully even you can see that your comment
"For some reason it doesn't work cause I assume that the variable isn't global" is pretty troublesome because you are not clear where the variables you need are located. And from this thread you can probably tell that there is something up with using globals. And yours is a good example of the problems of globals. Because they are not limited in scope and can be potentially be used in many unrelated pieces of code -- they can make it difficult to know where the cause of the problem is.
Post some code and perhaps we can show you how to change to not need globals.
Posted: Tue Feb 28, 2006 10:50 pm
by Benjamin
Jcart wrote:
agtlewis wrote:EasyGoing wrote:Well apparently I'm doing something wrong. I am setting the $GLOBAL['error_msg'] like so.
Global variables won't be transferred to another script. You may want to put it in a Session.
What do you mean? Of course they will as long as the script has been included at some point or another during execution.
What I mean is that if you set a global variable to a value in 1 page such as $GLOBALS['ERROR'] = "Blah"; and then do a header redirect to another page, that variable no longer exists, which I believe is why this dudes script doesn't work and it's also why he should use a session.
Posted: Wed Mar 01, 2006 12:46 am
by JellyFish
Yes agtlewis Your right on. The whole time I thought $GLOBALS would make you able to take a var and have itavialable to all scripts. Is there another way to make a var avialable without making it a session? This would help me very much, but if not I'll just have to make an exeption. Thanks.
Posted: Wed Mar 01, 2006 12:51 am
by feyd
what's the problem with using sessions?
Posted: Wed Mar 01, 2006 1:07 am
by JellyFish
I guess your right there is really nothing wrong with using sessions. But if there is another way I think it'll be good to expand my knowledge of this situation. If there is then I'd try it, but if not I'd just use sessions.
Posted: Wed Mar 01, 2006 1:16 am
by Christopher
EasyGoing wrote:Is there another way to make a var avialable without making it a session? This would help me very much, but if not I'll just have to make an exeption.
There are three basic ways to persist data in PHP:
1. Pass the values between scripts via HTTP
2. Put the values into a Cookie and have the client persist them
3. Have the server persist the values via the Session system (or in conjunction with it and a file, database or shared memory scheme).
Posted: Wed Mar 01, 2006 1:52 am
by JellyFish
Well how can I just make a variable global to where all scripts on my page can access the value without having to use a session? I think there's some way to do this like:
And I think this is one way to make it global and if not then you can ether correct me on this or insist that I use sessions.
Thank you guy's for all of your time and help I really appreciate all these post, I'm rapping this up with this post and all later post are appreciated. Thanks all and good night, well at least where I am.

Posted: Wed Mar 01, 2006 4:41 am
by Benjamin
What your trying to do is not possible because all of the variables are released from memory once your script is finished executing. The only other way you can do it without using a session or cookie is to redirect to the page with a url like this:
Code: Select all
header("Location: http://yourdomain.com/thispage.php?message=" . urlencode($message));
Then in the receiving page you can retrieve the message:
Code: Select all
$message = strip_tags($_GET['message']);
I haven't tested that and I would never use it myself but that is a way it can be done.