PDA

View Full Version : Session Expiry


kjc
09-07-2002, 04:16 PM
I have created a shopping cart for a web site I am designing. The shopping basket is stored in an array and is a global variable that is registered as a session. This seems to work fine, except that after a certain period of time the session or global variable will expiry and so I lose the shopping basket information.

Any ideas. I can't change the PHP configuration as this is hosted with an isp company.

Spookster
09-07-2002, 05:52 PM
A session stays open until the browser is closed. There is a garbage collection feature in the php.ini configuration file call gc_maxlifetime and that is usually set to 1440 seconds(24 minutes) but that destroys the session only if the session is inactive for that period of time.

Are you sure you aren't destroying the session accidentally somewhere?

What is the certain period of time that you refer to? How long? Does it happen on a certain page or anywhere?

whackaxe
09-07-2002, 06:20 PM
you should use a cookie to store shopping cart info every time the script runs so that you dont loose it

Spookster
09-07-2002, 07:59 PM
Originally posted by whackaxe
you should use a cookie to store shopping cart info every time the script runs so that you dont loose it

You should be more specific...client side cookies? Sessions work using server-side cookies and depending on how you use them they can store data on the client machine in a cookie as well.

whackaxe
09-07-2002, 09:07 PM
sorry, client side session only or with an futur expiry date. so that way PHP wront delete it

kjc
09-14-2002, 11:37 AM
Is there any way to change the session expiry time when creating the session ?

kjc
09-14-2002, 07:43 PM
OK. further to previous post....

I have run phpinfo and the session expiry is set to 180 minutes, yet still after if I leave the browser inactive for a certain amount of time(under 30 minutes), i come back and view the shopping cart and the value for the basket has dissappeared(which is registered as a session).

Any ideas.

Spookster
09-14-2002, 08:38 PM
Kind of hard to say without seeing any coding that you are using. Fact is a session doesn't expire until the browser is closed unless you create some kind of timer method to destroy it after a certain time. And a session isn't automatically destroyed by the garbage collector until it has been inactive for the amount of time set in the configuration.

kjc
10-03-2002, 03:38 PM
At the start of every page i have session_start();

The contents of the shopping basket are created in an array and stored as a variable as basket. When the basket array is first created it is registered as a session using session_register("basket");

This seems to work but at random times the basket variable will dissapear and so all the shopping basket is errased. It just happens at infrequent times and is not that common. Why??? Is there any other way i should do it. It happened just now and I had only be on the site for 5 minutes so it cant be the session expiry.

Any ideas?

firepages
10-03-2002, 04:56 PM
session_set_cookie_params(3600*100);

now the session 'should' last 100 hours IF the user has cookies enabled and IF your server does not override the garbage collection routintes

kjc
10-03-2002, 04:59 PM
firepages,

I amnot using cookies to store the basket. I thought that the storing the shopping cart as an array and having then session_register("basket"), would be ok. And in the most cases it is, just occasionally the basket variable and its contents dissapear...

KJC

mordred
10-03-2002, 05:22 PM
How do you pass the session_id then, if not by cookies? Looks to me that you have a problem with the automatic URL rewriting functionality or that you forget to add the session id to a link somewhere in your code.

kjc
10-03-2002, 05:26 PM
Originally posted by kjc
At the start of every page i have session_start();

The contents of the shopping basket are created in an array and stored as a variable as basket. When the basket array is first created on the browse shopping cart it is registered as a session using session_register("basket");




The code for creating the basket is :

if($new) //they are adding a product which requires the product id to be inserted into the temporary table
{
if(!session_is_registered("basket"))
{
$basket = array();
session_register("basket");
$items = 0;
session_register("items");
$total_price = "0.00";
session_register("total_price");
}
if($basket[$new])
$basket[$new] = 1;
else
$basket[$new] = 1;
}
if($delete)
{
unset($basket[$delete]);
}

_____________________
//When adding a product to the basket thelink to the basket page would be basket.php?new=(product_ID);

same as when deleting a product the link would be basket.php?delete=(product_ID)

And I have checked each page to ensure that the start of it has session_start();

:(

Hmmm. Any ideas anyone

mordred
10-03-2002, 05:35 PM
Originally posted by kjc
if($basket[$new])
$basket[$new] = 1;
else
$basket[$new] = 1;


Don't know if it's related to your problem or not, but what is the reason behind this code? For me it looks as if a new $basket element gets added regardless of what the user has chosen.

kjc
10-07-2002, 11:35 AM
That last code, is because the shopping cart should only have one item of each product, and not allow two of the same products to be purchased.

Another way rather than storing the array in a session would be to use the MySQL database...

I am thinking of doing it this way, as the session_register does not seem to be reliable enough. Also if the browser takes their time (>3 hours) then the session would expiry anyway.

When a browser firsts visits a PHP site, a PHP session is created.(?). Would it be possible to use this as the key to storing items in a basket table in the database. Then the table would contains basket addtions/deletions. When the user has confirmed the purchases they wish to make the data would then be taken out of the basket table and inserted into the permanant one...

Am I right in thinking that the session number created by PHP will be valid for the lenght the browser window is open and as such does not have an expury time?

Thanks

KJC

kjc
10-07-2002, 02:02 PM
Or further to my previous post, does anyone see any problems with using someones IP address as a temporary key to storing shopping cart items in a MySQL table?