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
https://forums.phpfreaks.com/topic/74730-php-session-question/
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
https://forums.phpfreaks.com/topic/74730-php-session-question/#findComment-377777
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.