开发者

Using PHP Sessions

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


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜