Eos Posted October 25, 2007 Share Posted October 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/74730-php-session-question/ Share on other sites More sharing options...
soycharliente Posted October 25, 2007 Share Posted October 25, 2007 You can just use session_start() as a stand alone. session_start() creates a session or resumes the current one based on the current session id that's being passed via a request, such as GET, POST, or a cookie. Quote Link to comment https://forums.phpfreaks.com/topic/74730-php-session-question/#findComment-377766 Share on other sites More sharing options...
Eos Posted October 25, 2007 Author Share Posted October 25, 2007 I see. One moment i'll re-work it and re-post what i've got, let me know if i'm on the right track. Quote Link to comment https://forums.phpfreaks.com/topic/74730-php-session-question/#findComment-377771 Share on other sites More sharing options...
dbo Posted October 25, 2007 Share Posted October 25, 2007 yeah just put session_start() at the very top of all your pages. Quote Link to comment https://forums.phpfreaks.com/topic/74730-php-session-question/#findComment-377772 Share on other sites More sharing options...
soycharliente Posted October 25, 2007 Share Posted October 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/74730-php-session-question/#findComment-377777 Share on other sites More sharing options...
dbo Posted October 25, 2007 Share Posted October 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/74730-php-session-question/#findComment-377778 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.