Jump to content

can't retrieve from glabal variable


cflores59

Recommended Posts

I am presuming that I set a global variable so that I can retrieve it on a different page...My filename is userid; and I will need this in many different pages. I set it thusly:

 

mysql_select_db($db , $link) or die("Couldn't open $db: ".mysql_error());
$long=$_SERVER['PHP_SELF'];   //get path
$text = substr(strrchr($long, "/"), 1); //get file name+extension
$UserID = trim($text, " .php"); //remove extension, leaving userid
Globals["UserID"];

 

and in a different file try to retrieve it thusly:  echo "user is ".$GLOBALS["UserID"];

but all I ever see is "user is"------any help appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/227677-cant-retrieve-from-glabal-variable/
Share on other sites

You got the wrong idea. You can set/use global variables so you can use them in different scopes within the same execution. I.e. from one function to another. However, using global variables is typically not the best method of doing that. Instead you can just pass the variables from one function to another.

 

However, in your case, you want a variable created on one page load to be available on another page load. You need to use either session variables (if you only need then for the current session) or cookies (if you need them to persist for multiple sessions).

Took me awhile to figure out WHERE in the code to actually place the session start, but now it works fine==thanks sooooo much!!!

 

Also, the reason I didn't get it to work the first time was because I didn't understand that session-start was needed in EVERY page it is referenced. This is not intuitive (to me) as it implies the start of a new session on every page. So thanks for making that clear. It did the trick!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.