[SOLVED] $GLOBALS not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

[SOLVED] $GLOBALS not working

Post 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
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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']
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

No. its $GLOBALS
$_SESSIONS[] is not working too. Same problem
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

which version of PHP are you running?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

4.3.7

Post by anjanesh »

4.3.7
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

4.3.7

Post by anjanesh »

PHP version 4.3.7
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Version : 4.3.7
Something wrong with posting
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Version 4.3.7
something terribly wrong with posting ?
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post 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...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

lol sessions r obviously working.....

php.net/sessions
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
Post Reply