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
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).

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.