tags:

views:

50

answers:

1

Hi All,

So, I've been playin' around with sessions in PHP today, and after procrastinating over whether I should use sessions or not since I started PHP about 6 months ago (it looked scary), I've found it to be quite simple. But I am using time() as a session id, I'll explain why...

I found a page on session reference in php.net website, and one of the code samples uses this to manage sessions:

session_start();

if( !isset($_SESSION['last_access']) || (time() - $_SESSION['last_access']) > 60)
 $_SESSION['last_access'] = time();

However, this expires very quickly. Ofcourse, I COULD change 60 to a very high number, but this is for a website where customers will be spending on average, 3 - 4 hours just adding products to the shopping cart, so I can't have sessions expire unless they close the page.

How can I transfer the same session id over to all pages on our site regardless of time(). I don't really want to be using time to manage session id's.

I DID use the constant SID in a url like this:

<?php echo '<a href="theotherpage.php?sessid='.SID.'">go to another page</a>'; ?> however, using the constant, as advised by the PHP website, does not work.

Can someone please help me maintain the same session id, regardless of time, across the whole site?

Your help is much appreciated, and thanks! :)

+3  A: 

I think you may have a misunderstanding of sessions. Assuming, their cookies are enabled, you will never need to use a session ID. The session ID itself is stored in a cookie. If you would like to keep a session alive longer, simply use ini_set('session.gc_maxlifetime', 20); and change 20 to the amount of minutes you would like it alive for.

Please keep in mind you must use start_session(); at the very top of each file to make sure that specific file uses sessions. (This would be a good reason to have 1 main included config file at the top of the php files, so u can easily add that to the 1 file and it is added to all pages)

Anthony Greco
When users add products to the cart, I am using the session id as an id for that particular visitor in a database, which is why I have done it this way. And I have no say in the matter either, I have been ordered to do it this way unfortunately.
lucifer
Oops. I AM using session_start(), I just forgot to paste it in the question.
lucifer
I have never actually used that, but you can get the session id of their current session by calling session_id(). Again, assuming they have cookies enabled, the way u keep a session open as long as u want is by the ini_set function. This allows u to tell it to keep the session open till you want it to close.
Anthony Greco
Thanks anthony! :D
lucifer