Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Globals on PHP

Status
Not open for further replies.

Mellegem

Technical User
Apr 3, 2003
49
ZA
Hi all,
I made a post earlier this month about no being able to conect to a mysql dbase. Since then I've worked out that it had nothing to do with mysql but I think it is in the PHP setup or something ****my head hurts ****

Heres the story, we use global variables that work in a basic test program as is shown in the php manual. There is a config file that is included. register globals is set to on.

This all works on the M$ machine but when I transferred the code onto the Debian Woody box the global variables in the config file don't transfer to the page that is calling it, however the included functions on the page are referenced. This caused the problems connecting to the dbase. [pc]
Now hardcoding the database specific variables into the pages made the page connect to the dbase but global variables still aren't being passed between functions. I have spent ages comparing the windows and linux php.ini files but they both have the same variable settings including register_globals on.

Help anyone, does this make sence?? [bravo]

Any Ideas, I hope this made sence.
 
Mellegem,

As I understand it register_globals ON or OFF has no bearing on the availability of the variables within functions. The variable availability follows the general variable scope of PHP.

Now, that's how it should work, however, you state that for some reason the M$ machine provides the globals within functions.

If you set your vars into the $GLOBAL array, then you can access them anywhere using $GLOBAL['myvar']. The $GLOBAL array is superglobal, so it can be accessed anywhere.

For db Connection I actually define constants, which are also superglobal. The information about the host,user and password doesn't change within the script - in fact I don't want to have it changed, so I define them.

Code:
define('HOST','myserver.com');
define('USER','public');
define('PASS','myPass');

The code above is stored in an include file outside the document root of the web server to make web access impossible.
 
Hi
When I say global variables we are using the form

Code:
$a=1;$b=2;
function add(){
  echo $a + $b;
}

This snippet of code , or one simmilar works on both configurations.

The define functions, although I havn't understood them till now are being used for DB connections yielding the same results.... nothing [nosmiley]

 
$a in your example exists in two realms, namely outside the add function and inside.

Since you make no reference to the 'outside' $a within your function $a inside is a new, local, variable that is not defined, hence '0'.

To refer to $a in the outside contect you need either to:
-> pass it as an argument
-> globalize $a within the function
-> make a global var $GLOBAL['a'] and refer to it

Code:
$a=1;$b=2;
function add(){
  global $a, $b;
  echo $a + $b;
}

register_globals only translates the input variable stream (GET,POST,COOKIE) into variables that are named by the keys of the associative arrays.
It is best and most safe practice to refer to input stream variables through the superglobal arrays $_GET,$_POST,$_COOKIE.

 
How can there be any problems with the code if it works on a Microsoft Server but not on a Debian server? I posted the code as a rough example to show what does and doesn't work.

Are there issues with variables that differ between Microsoft and Linux, and if so, how can PHP claim to be platform independant? Both configurations run on Apache, could the Apache setup have any bearing on this problem?? [noevil]
 
Well, I think the point is the function you posted shouldn't work on either platform :)

However, DEFINE should work like you're requesting. How are you using define completely? It should be just like so

Code:
DEFINE("DATABASE", "some db name");

echo DATABASE.&quot;<br>&quot;;

For example. The reason it's useful looking at the code is because if the code is working one one and not the other, then it's more likely that the code is broken and you're getting lucky on one.

I don't see how Apache could make a difference in this case.... can you give us the offending code?

Actually, after typing all that it sounds like your problem might be an access problem, what's the read access on the file/directory on the Debian server? And what user is apache running as? On the Win box it's running as System unless you've specifically stated otherwise, and will be able to read your entire file system, on my Debian box if I remember right, that was not the case... that box has since gone the way of the dodo so I'm not certain though. You can test it pretty quickly by having your include file spit out some output and see if that shows up.


-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top