Jump to content

Recommended Posts

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 by xExekut3x
Link to comment
https://forums.phpfreaks.com/topic/280091-php-sessions/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/280091-php-sessions/#findComment-1440426
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/280091-php-sessions/#findComment-1440429
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.