onlysumitg Posted May 19, 2010 Share Posted May 19, 2010 Hi all I am new to and PHP. So what i am tring to do is like below 1. I set a variable in $_SESSION array. This variable actually is SQL Connection object. 2. Then i show a link on web . which redirect to other location. like <a href="\newaction?param=xyz">click here</a> 3. When now when i try to access variable in stored in $_Session in "newaction" i got nothing.. my variable is not there.. Please help me Thanks in Adv Quote Link to comment https://forums.phpfreaks.com/topic/202304-_session-geting-lost/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2010 Share Posted May 19, 2010 This variable actually is SQL Connection object. There are two reasons this won't work - 1) Resources (database connections) cannot be serialized (which is how session data is stored/retreived, and 2) All resources (no matter what variable they are stored in) used on a page are destroyed when the script on that page ends. Quote Link to comment https://forums.phpfreaks.com/topic/202304-_session-geting-lost/#findComment-1060821 Share on other sites More sharing options...
onlysumitg Posted May 19, 2010 Author Share Posted May 19, 2010 thanks for reply.. so is there any way i can pass that SQL connection object to other page? Quote Link to comment https://forums.phpfreaks.com/topic/202304-_session-geting-lost/#findComment-1060822 Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2010 Share Posted May 19, 2010 If you are asking about class variables other than the actual resource link to the database, the answer is yes. Just make sure that the class definition exists before the session_start() statement. Quote Link to comment https://forums.phpfreaks.com/topic/202304-_session-geting-lost/#findComment-1060825 Share on other sites More sharing options...
JakeJ Posted May 19, 2010 Share Posted May 19, 2010 Rather than using the MySQL connection string in a session variable, I put my connection string in its own file and then use that file as include wherever I need it. And if I need to change the connection string, I only need to do it in one place. Is there a particular reason you're putting it in a session variable? The only reason I can think of to put it in a session variable is if it's going to change quite a bit. In any case, do you have session_start() in the other page? If not, it won't inherit the $_SESSION[] array. If those things don't solve the problem, post some code. Quote Link to comment https://forums.phpfreaks.com/topic/202304-_session-geting-lost/#findComment-1060826 Share on other sites More sharing options...
onlysumitg Posted May 19, 2010 Author Share Posted May 19, 2010 what i am tring to save is mysqli object like $mysqli = new $mysqli(XXXXXXXX,XXXXXX,XXXXXX); $_SESSION['con'] = $mysqli <a href="/gohere.php?abc=abcc">click here </a> ... ... in gohere.php echo "<pre>"; print_r($_SESSION); echo "</pre>"; ..... i get blank page .. Quote Link to comment https://forums.phpfreaks.com/topic/202304-_session-geting-lost/#findComment-1060831 Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2010 Share Posted May 19, 2010 You were already told why that does not work - This variable actually is SQL Connection object. There are two reasons this won't work - 1) Resources (database connections) cannot be serialized (which is how session data is stored/retreived, and 2) All resources (no matter what variable they are stored in) used on a page are destroyed when the script on that page ends. Even if you were on a server where persistent connections worked, you would still need to execute the code to create/get one of the persistent connections on every page load. Quote Link to comment https://forums.phpfreaks.com/topic/202304-_session-geting-lost/#findComment-1060838 Share on other sites More sharing options...
BizLab Posted May 19, 2010 Share Posted May 19, 2010 what i am tring to save is mysqli object like $mysqli = new $mysqli(XXXXXXXX,XXXXXX,XXXXXX); $_SESSION['con'] = $mysqli <a href="/gohere.php?abc=abcc">click here </a> ... ... in gohere.php echo "<pre>"; print_r($_SESSION); echo "</pre>"; ..... i get blank page .. lookup a php "include" file used as such : require('my_connection_vars.php'); and int that script, place all your connections as follows: define(DB_HOST, 'localhost'); // defined as constants or not define(DB_NAME, 'localhost'); define(DB_USER, 'localhost'); define(DB_PASSWORD, 'localhost'); // or variables $DB_HOST = 'LOCALHOST'; $DB_NAME = 'my_new_db'; $DB_USER = 'username'; $DB_PASSWORD = 'abc123'; fill in your info of course. Include this file right after your session_start(); You can define(DB_HOST, 'localhost') as constants or not. it doesnt; really matter. I wouldnt store that info in a session var. Session vars can be used in other ways and help to store data that has been queried from a database and you would like to reduce the number of redundant queries while the user navigates... information like "type of user" Used as follows: $_SESSION['user_type'] = $row['user_type']; Now you won't have to re-query user type on every page you need the info! word. Quote Link to comment https://forums.phpfreaks.com/topic/202304-_session-geting-lost/#findComment-1060842 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.