views:

211

answers:

4

I have written a script that gets paypals current rates compared with the dollar every hour (the currency my products are in by default).

The user can set their currency in their settings and that is store in the user table.

My initial idea was to store all the currencies rates in the database and then when the user logs in store the currency code and rate in their session. Then around each price I have a function that multiples the price by the users rate and appends the currency code on the end.

My only worry being is that the session variable may exist for sometime and could potentially make the price completely wrong.

Rather than store the rates in the session, should I just store their currency code and store the rates in a memory table or on the file system for fast access and have the price conversion function access it? So the prices are as up to date as the rates.

How is this normally achieved?

+6  A: 

Could you not instead show the prices in the currency they're priced in, and show an approximate price in the user's selected currency, with the caveat that the actual rate may vary by the time they checkout?

Rowland Shaw
yes, I had considered that. I can keep it simple then too... wouldn't really matter if the rates were updated once a day
Mark
I've certainly seen this approach taken on some online stores that I've been browsing, so there is a precedent.
Rowland Shaw
This is how ebay does it for example.
Wally Lawless
Even if you have up-to-the-minute exchange rates, the user is charged a rate from their bank, which most likely will be different to your rate anyway. It's just a guide, so I wouldn't sweat it.
nickf
A: 

You can change the default lifetime of a session - it's stored in the php.ini variable session.gc_maxlifetime. The default is 1440 (24 hours), after which the session will be cleared up by the following garbage collection.

The variable can be edited directly (in the file) or with ini_set:

ini_set('session.gc_maxlifetime', 60); // set to 1 hour

Edit

You can increase the probability of the garbage collection running on any given call using the variables session.gc_probability and session.gc_divisor. Documentation is at http://www.php.net/manual/en/session.configuration.php

adam
A: 

Lets do the Jeopardy thing.

Is it possible to use $_SERVER as a application-wide global object, similar to ASP's Application object? If not, is there a PHP application object?

If so, you could store the currency rate in $_SERVER, then update it when necessary (e.g. when Paypal updates it's currency rate)

gamers2000
+2  A: 

Just cache the calls that gets the Exchange Rates from PayPal. That's all there is to do. As long as the cache is not stale, your users will multiply with the cached values. If the cache goes stale, the new rate will be fetched, e.g. (faux code)

$currency = 'usd';
if (!$cache->has("exRate-$currency")) {
    $exRate = ForEx::find($currency);
    $cache->save("exRate-$currency");
} else {
    $exRate = $cache->get("exRate-$currency");
}
CurrencyConverter::setRate($currency, $exRate);
CurrencyConverter::convert(100, 'eur', 'usd');

For caches use APC or memcached.

Gordon
Thank you for your input. I think this is a good solution. I have not implemented a cache at the moment...when I do I will look back at this solution.
Mark