xExekut3x Posted July 12, 2013 Share Posted July 12, 2013 (edited) I'm using MySQL to store Session data. I understand that "session_start();" has to be called before anything else. Are there no exceptions? Can I not open up a database connection and pass it to my Session class before I call session_start();? I'm trying to cut down on the number of times I open up SQL connections in my script, and if I could open up just one connection per user, that would be great. Edited July 12, 2013 by xExekut3x Quote Link to comment https://forums.phpfreaks.com/topic/280091-php-sessions/ Share on other sites More sharing options...
requinix Posted July 12, 2013 Share Posted July 12, 2013 You must session_start() for PHP to do session stuff for you. If you want to manually read the session cookie, query the database, and reconstruct data, that's stupid but you can go right ahead. As for opening up multiple connections, you're the one in charge of when and how that happens so just make sure you reuse (either explicitly or by passing the same information to most database drivers' connect methods) the connection for both sessions and normal usage. Quote Link to comment https://forums.phpfreaks.com/topic/280091-php-sessions/#findComment-1440426 Share on other sites More sharing options...
kicken Posted July 12, 2013 Share Posted July 12, 2013 I understand that "session_start();" has to be called before anything else.Should read: ... called before anything else any output is generated. You can do whatever you want prior to calling session_start() so long as a) No output is generated and b) You're not trying to use session data Opening a database connection shouldn't conflict with either of the above so feel free to open the DB first, then call session_start. Quote Link to comment https://forums.phpfreaks.com/topic/280091-php-sessions/#findComment-1440429 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.