Re: Accessing cookies in required files
| From: | Richard Lynch | Date: | Sun, 30 Jun 2002 22:46:25 +0000 |
| Subject: | Re: Accessing cookies in required files | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-104767@lists.php.net to get a copy of this message | ||
In article <20020627082709.78063.qmail@pb1.pair.com> ,
chris.morrow@btopenworld.com (Chris Morrow) wrote:
>Up until now this has worked fine. But now I have a function in the
>settings.inc file that tries to read the value of a cookie called
>"testcookie". My problem is that the settings.inc file doesn't seem to be
>able to access the cookie at all. But if I try to access it direct from a
>page rather than the required file it works fine.
It has nothing to do with the require and *everything* to do with the
FUNCTION.
You see, it's a really Bad Idea (tm) to have your functions use variables
from the "outside" world -- variables should be passed in as parameters.
function foo (parameter, parameter, ..., parameter){
.
.
.
}
Inside the function "foo" there are *NO* variables except the "parameter"
variables.
You can over-ride this, though, using "global"
function foo (parameter, parameter, ..., parameter){
global $cookie1;
.
.
.
}
EXCEPTION:
The new-fangled "superglobals" $_POST, $_GET, $_COOKIE etc are global
everywhere.
So you *could* use $_COOKIE['cookie1'] without using "global".
This rule about variable scope is a FEATURE -- Without it, the function is a
relatively weak little thing, and you are all to likely to have stupid,
stupid, stupid bugs from variable name conflicts and a million lines of PHP
code on your web-server to find the mistake... With the rule that only the
paramters (and globals) can exist inside a function, you limit your problem
search to a very much smaller space.
--
Like Music? http://l-i-e.com/artists.htm