Page 1 of 1

[SOLVED] $GLOBALS not working

Posted: Tue Jun 22, 2004 11:49 am
by anjanesh
I have this in test.php

Code: Select all

<?php
$GLOBALS['test']="anj";
echo '<A HREF="page.php">Jobs</A>';
?>
and this in page.php

Code: Select all

<?php
echo $GLOBALS['test'];
?>
It gives : Notice: Undefined index: test in page.php on line 2
register_globals = On
Still I cannot get to access superglobals
Anyone ? Thanks

Posted: Tue Jun 22, 2004 12:13 pm
by dull1554
any reason you want to use globals?
why not sessions

but should it be

Code: Select all

$_GLOBALS['blah']//instead of
$GLOBALS['blah']

Posted: Tue Jun 22, 2004 12:46 pm
by anjanesh
No. its $GLOBALS
$_SESSIONS[] is not working too. Same problem

Posted: Tue Jun 22, 2004 1:20 pm
by patrikG
which version of PHP are you running?

4.3.7

Posted: Tue Jun 22, 2004 1:27 pm
by anjanesh
4.3.7

4.3.7

Posted: Tue Jun 22, 2004 1:27 pm
by anjanesh
PHP version 4.3.7

Posted: Tue Jun 22, 2004 1:30 pm
by anjanesh
This is strange. When I posted the numbers (4.3.7) alone it kept giving the message : Failed Sending Email
I did not know what was wrong and figured the post was not posted so I repeatedly posted it and got the same error. Now I look at it and find that its posted all right. Moderators(or whoever) : need to fix this.

Posted: Tue Jun 22, 2004 1:33 pm
by anjanesh
Version : 4.3.7
Something wrong with posting

Posted: Tue Jun 22, 2004 2:08 pm
by anjanesh
Version 4.3.7
something terribly wrong with posting ?

Posted: Tue Jun 22, 2004 2:14 pm
by wwwapu
In php manual it says $GLOBALS:
An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.
Note the "global scope of the script" You had two scripts instad of one. I suppose $GLOBALS is useful with your own functions:

Code: Select all

<?php
$foo="bar"

function printfoo(){
print $GLOBALS["foo"]
}

printfoo(); //$foo is printed
?>
To use sessions you have to session_start() each script and set the variable into array with $_SESSION["something"]=$foo.
And so on...

Posted: Tue Jun 22, 2004 5:42 pm
by John Cartwright
lol sessions r obviously working.....

php.net/sessions

Posted: Wed Jun 23, 2004 10:20 am
by anjanesh
I was trying to send a variable from one script to another using $GLOBALS since $_SESSION was not working.
Now I got it. I was using sessions in db (session_set_save_handler('user')) which does not store it in a file since its set to db. Therefore I had to get it in the session_read() function (user defined). All this time I thought I could just send a variable from one script to another just like that. But it stores it in the session file (if files is default). Otherwise get it from read() and store it manually into the db.