Jump to content


PHP && Sessions

  • Please log in to reply
2 replies to this topic

#1 xExekut3x

  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 12 July 2013 - 05:12 AM

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, 12 July 2013 - 05:13 AM.

#2 requinix

  • Administrators
  • Lazy Administrator
  • 9,109 posts
  • LocationWA

Posted 12 July 2013 - 05:41 AM

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.
The Reimann Zeta Function Trolley Problem | "Stop! You're spoiling my unjustified indignation!"

#3 kicken

  • Gurus
  • Wiser? Not exactly.
  • 3,280 posts
  • LocationBonita, FL

Posted 12 July 2013 - 06:28 AM

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.
Recycle your old CD's, don't trash them!
Did I help you out? Feeling generous? I accept tips via Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users