cirma Posted June 16, 2010 Share Posted June 16, 2010 Hi, I'm using fsockopen() to call a web service. The service simply looks up a surfer's IP address and returns their city with whatever accuracy. We get charged per query...so if a surfer refreshes the page 100 times, we get charged 100 times. Don't ask me why we're not using another solution not involving a web service, I didn't make that decision Anyway, I need to prevent that from happening but still allow the surfer to refresh the page 100 times. So what I want to do is cache the surfer's IP address and city after the lookup so that we only ever get charged once per IP within a given day. I was thinking of simply calling the webservice, storing the IP and city name in a mysql database, then every time a surfer visits the page, checking to see if we have the IP stored to begin with. If not, call the web service, otherwise, retrieve it from the db. Is there a more elegant solution than this? We'll be doing about 9-10,000 lookups per day, and I'm not sure if there's a way to cache the responses with less performance hit, like not by using mysql? Thanks. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted June 16, 2010 Share Posted June 16, 2010 try something like this: set a session variable, that once set flags the system to not re-check the IP: <?php session_start(); if (!isset($_SESSION['ip_checked']) || $_SESSION['ip_checked'] != true){ //run the check } Now, on the script that does the check with fsockopen() <?php session_start(); if (!isset($_SESSION['ip_check']) || $_SESSION['ip_check'] != true){ // check them $_SESSION['ip_check'] = true; $_SESSION['location'] = $LOCATION_FOUND_FROM_WEB_SERVICE; } Complete pseudocode, but it should point you in the right direction. Quote Link to comment Share on other sites More sharing options...
cirma Posted June 16, 2010 Author Share Posted June 16, 2010 That's a great idea, thanks...I'll use session variables Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted June 16, 2010 Share Posted June 16, 2010 While sessions will work, and possibly suffice for your application, they will only be good for as long as the user's browser session is open. So, if that user visits the site 5 times in a day, closing their browser window/tab in between each visit, you get charged 5 times for that user. Consider actual caching using Cache Lite. It's very, very simple to setup and you can set cache limits for however long you want per user. I use it all the time for any API/cURL calls I make to avoid unnecessary external calls being made. Call once, display 100 times. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.