cflores59 Posted February 14, 2011 Share Posted February 14, 2011 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 15, 2011 Share Posted February 15, 2011 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). Quote Link to comment Share on other sites More sharing options...
cflores59 Posted February 15, 2011 Author Share Posted February 15, 2011 Actually, I had tried that first. $_SESSION['UserID'] = trim($text, " .php"); //remove extension, leaving userid and echo "user is ".$_SESSION['UserID']; Feeling dumb... Appreciate your help Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 15, 2011 Share Posted February 15, 2011 You need to use <?php session_start(); ?> at the beginning of each script where you want to use session variables. Ken Quote Link to comment Share on other sites More sharing options...
cflores59 Posted February 15, 2011 Author Share Posted February 15, 2011 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.