Jump to content

PHP Session Question


Eos

Recommended Posts

Hey Guys,

 

I'm relatively new to php and this is the first time i've tried to use php sessions. I'm attempting to create a semi e-commerce type page. Below is the code i'm using to set the session up, and, I believe I may have the wrong mentality about php session functionality.

 

if(!isset($_SESSION)) {
    if (session_start()) {
      mysql_query('INSERT INTO rooms ( created ) VALUES ( \''.date('Y-m-d H:i:s').'\' );');
      if ( $fr_room = mysql_fetch_assoc(mysql_query('SELECT roomid FROM rooms ORDER BY created DESC LIMIT 1;')) ) {
        $_SESSION['roomid'] = $fr_room['roomid'];
      }
      else {
        $_SESSION['roomid'] = -1;
      }
    }
  }

 

The mysql stuff in this example doesn't really matter, but my route of thinking is, the first thing this page does is check to see whether $_SESSION is set, effectively determining whether I session is active, if it isn't, start the session and set up some $_SESSION variables. But whenever I load the page, even if the session has already started, $_SESSION still isn't set and it goes ahead and inserts another value into the rooms table. (PS, roomid in this example is similar to a shopping cart id, so obviously I cant have it incrementing every time the page loads!)

 

Am I doing something wrong or, like I mentioned earlier, do I have the wrong mentality of how php sessions are supposed to work?

 

Any advice would be greatly appreciated, thanks.

Link to comment
Share on other sites

yeah just put session_start() at the very top of all your pages.

 

If you're going to do that, I would use an include or something. I usually have an include or require at the top of all my pages that includes a file that I can dump important things into that need to go on every single page.

Link to comment
Share on other sites

If you have other stuff that is relevant that's not a bad idea. If not, it is, because:

 

session_start();

 

is easier to type than

 

include_once "someheaderfile.php";

 

and if all you had was session_start in this file then it would actually be slower doing the include.

Link to comment
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.