bwspot Posted April 5, 2013 Share Posted April 5, 2013 (edited) I have auth page that auth user against php and later registers data from mysql querry into the session varriables like this: //register sesssion authenticatedelse {$_SESSION['authenticated']=true;//register other values$sessionid=session_id();$_SESSION['phonenumber'] = mysql_result($result,0,'phonenumber');$_SESSION['username'] = mysql_result($result,0,'username');$_SESSION['userlevel'] = mysql_result($result,0,'userlevel'); //session_register("userlevel");//session_register("email");//session_register("sitename");//session_register("ipaddr");//session_start(); - old and replaced by below line $session = array(); after all is done the user is redirected back to: header("Location: main.php?".Session_Name()."=".Session_ID()); and in main.php varriables are called like: dbConnect("corporate");$mysql = "select * from users WHERE phonenumber = $phonenumber";$rs = mysql_query($mysql);$row = mysql_fetch_array($rs);echo "phone is";echo $phonenumber;echo "global phone is ";echo $GLOBALS['phonenumber']; Problem is that after register_global was removed from php above $phonenumber varriable is empty. I have seen this link: http://uk3.php.net/manual/en/function.session-register.php but i have hard time understanding how to fix above. Every page has a session_start() at the beggining. Can anyone help how to fix my script with the least changes? I am using php from time to time so forgive me my lack of expertise. thx Edited April 5, 2013 by bwspot Quote Link to comment https://forums.phpfreaks.com/topic/276555-how-to-register-_sessionvar-globally/ Share on other sites More sharing options...
exeTrix Posted April 5, 2013 Share Posted April 5, 2013 I'm not seeing the point in the global phone number. You have the phone number set in session data ($_SESSION['phonenumber']). The only reason globals are pseudo useful is because variables can be used within class context without having to be passed in. If this phone number is a company number and will be constant then you'll be better off adding as below: define( 'COMPANY_PHONE', 0145875581144 ); echo COMPANY_PHONE; Sorry if I've misunderstood but you're doing the right thing moving away from globals. Quote Link to comment https://forums.phpfreaks.com/topic/276555-how-to-register-_sessionvar-globally/#findComment-1423017 Share on other sites More sharing options...
bwspot Posted April 5, 2013 Author Share Posted April 5, 2013 The phone number is pulled from the data based based on the username that authenticates. So it is different for every user. After the user is redirected the $phonenumber variable is empty. the referenced paga says: "Caution This registers a global variable. If you want to register a session variable from within a function, you need to make sure to make it global using the global keyword or the $GLOBALS[] array, or use the special session arrays as noted below. " I tried to add: global $phonenumber but the varriable was still empty after redirection Quote Link to comment https://forums.phpfreaks.com/topic/276555-how-to-register-_sessionvar-globally/#findComment-1423069 Share on other sites More sharing options...
jcbones Posted April 5, 2013 Share Posted April 5, 2013 $_SESSION is a super global already. If you notice, your above referenced link has a note: Notes CautionIf you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled. Quote Link to comment https://forums.phpfreaks.com/topic/276555-how-to-register-_sessionvar-globally/#findComment-1423106 Share on other sites More sharing options...
bwspot Posted April 5, 2013 Author Share Posted April 5, 2013 so why i cannot access it? Quote Link to comment https://forums.phpfreaks.com/topic/276555-how-to-register-_sessionvar-globally/#findComment-1423142 Share on other sites More sharing options...
Christian F. Posted April 5, 2013 Share Posted April 5, 2013 (edited) There are two things you need to take care of in order to use session variables: Always use session_start, before any content has been sent to the browser. Store the data in the $_SESSION array directly, do not use session_register. As long as you do both of these, and the user is not blocking cookies, sessions will work. Forget that session_register exists: It has been outdated and "deprecated" for a long time now. Edited April 5, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/276555-how-to-register-_sessionvar-globally/#findComment-1423156 Share on other sites More sharing options...
bwspot Posted April 5, 2013 Author Share Posted April 5, 2013 i do have session_start() at the begining of the script and I commented out the session_register. The part i dont understand is how to store the data in the session array ? My php page contains: session_start() $_SESSION['phonenumber'] = mysql_result($result,0,'phonenumber');$_SESSION['username'] = mysql_result($result,0,'username');$_SESSION['userlevel'] = mysql_result($result,0,'userlevel'); then i am not sure if this is what i need to store above in the array? $session = array(); if yes how do i read it later? thx and sorry for stupid questions but i am new to all this stuff and dont programm that often. Quote Link to comment https://forums.phpfreaks.com/topic/276555-how-to-register-_sessionvar-globally/#findComment-1423181 Share on other sites More sharing options...
Solution jcbones Posted April 5, 2013 Solution Share Posted April 5, 2013 //start session: session_start(); //add to session: $_SESSION['iAmAPhReak'] = 'My string of session data'; //echo the data: echo $_SESSION['iAmAPhReak']; //dump the data: echo '<pre>' . print_r($_SESSION,true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/276555-how-to-register-_sessionvar-globally/#findComment-1423199 Share on other sites More sharing options...
bwspot Posted April 6, 2013 Author Share Posted April 6, 2013 thx , above with http://www.phpriot.com/articles/intro-php-sessions/7 helped me to understand it and fixed my issue Quote Link to comment https://forums.phpfreaks.com/topic/276555-how-to-register-_sessionvar-globally/#findComment-1423231 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.