0

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! :)

4
  • 2
    You're misunderstanding what this code does. I just keeps the last access time updated in the session in 60 second intervals. It does not expire the session, and the session id is something completely different as well. Commented Oct 29, 2010 at 7:18
  • How do I get the session id then? And it it unique for every visitor of the website? Commented Oct 29, 2010 at 7:19
  • 1
    session_id() will get it Commented Oct 29, 2010 at 7:20
  • 1
    You can get the session id using session_id if you're interested in it, but it's something that's handled for you automatically and that you don't usually need to deal with. Commented Oct 29, 2010 at 7:20

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

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.
Oops. I AM using session_start(), I just forgot to paste it in the question.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.