Do you know a way to clean the browser cache by PHP code ?

Hi Experts

I'm working with "maps" at the site.
These maps are actualized from date to date. To have the maps actualized at the site its's needed to clean the cache, otherwise a
desactualized map appear.

Do you know a way to clean the cache by PHP f.e (or  any othe tecnology compatible with PHP)  code before the map is presented?
(or it is too much invasive and to be done the user's browse must to be configured to)

Thanks in advance.
Eduardo FuerteDeveloper and AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
No, you can't clean the user's browser cache, that would be considered a security risk.  There are various ways to get the browser to get a new page or image from the server.  The most common way is to create the link with a different query string every time like this where the 'q=' is followed by a unix timestamp:
http://www.mydomain.com/myimage.jpg?q=13456788

Open in new window

0
RobOwner (Aidellio)Commented:
Unfortunately, caching it controlled by the browser.  What you can do is send the no-cache / must revalidate header, which will clear the cache for YOUR site only (site where the php is served from)

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

Open in new window

1
mankowitzCommented:
You can never force the user to clear their cache, not for your own site, and not for any other.

However, you probably don't need to do that. If the information that you want to remove is stored in cookies, all you need to do is overwrite them with your code, e.g.

From http://www.php.net/manual/en/function.setcookie.php#73484

// unset cookies
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}

Open in new window


If cookies aren't the problem, you can do a "force reload" of your page which will reload all html, css, javascript, etc. using the following javascript command:

window.location.reload(true)

Finally, another way to do this is to add versioning to your filenames. One common method is to add a query string, but that doesn't always work. For example,

www.example.com?v=239487239

Then, when you need to reactualize the map, you issue a new URL, such as

www.example.com?v=239487240
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ray PaseurCommented:
The way that I solve this problem is twofold.  First, I use the cache-control and expires headers.  And I send an empty <span> to let a subsequent AJAX request send the visible data that populates the <span>.  This seems to work well for JavaScript-enabled applications like AngularJS.  For a jQuery example, see this article and look for "A Character-based CAPTCHA Test using AJAX to hide the CAPTCHA string."
http://www.experts-exchange.com/articles/9849/Making-CAPTCHA-Friendlier-with-Simple-Number-Tests-or-PHP-Image-Manipulation.html

A side-effect of this design is that it limits automated scraping of your HTML document.  The HTML document contains the markup, but none of the data.  The cookie requirement is not rigid if there is no need for session data, but the client must run JavaScript.
0
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Hello

I'm really in a hurry to deliver the task, with not to much time to deep in this subjsct by now.

window.location.reload(true)  make it unbearable slow in my case  and

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

Open in new window


was not efecctive in my test.
0
mankowitzCommented:
Then try the versioning approach. the easiest way to do that is find the url for the actualization of the map and append an random query string to it. This will work in most instances.
0
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Sorry I expressed me and thank you for your attention.

Just to complicate a little more the way the map is called is

<iframe width='105%' height='600px' frameBorder='0' src='https://a.tiles.mapbox.com/v4/amnajd.o10812h1/attribution.html?access_token=pk.eyJ1IjoiZWZ1ZXJ0ZSIsImEiOiJjaWVwcHhuOWkwMDFpd2drbWtmcHlrNTI0In0.Bx7VRSAwHbuEQtGyYiugBA'></iframe>

Open in new window


So.. if I put  ?q=13456788  representing a randomic number (f.e.)  after .html or at the end of the link the token became invalid!
0
Eduardo FuerteDeveloper and AnalystAuthor Commented:
"the way I expressed me"
0
mankowitzCommented:
I wasn't able to access your link, but the following did not give me an error

https://a.tiles.mapbox.com/v4/amnajd.o10812h1/attribution.html?q=23423&access_token=pk.eyJ1IjoiZWZ1ZXJ0ZSIsImEiOiJjaWVwcHhuOWkwMDFpd2drbWtmcHlrNTI0In0.Bx7VRSAwHbuEQtGyYiugBA
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Hello

Really, the way you post it's ok.
I clean the cookies too.

I don't know if mapbox delays a time to actualize the map on its site. The actualization is not instantaneous... I'm going to test a little more.
0
Eduardo FuerteDeveloper and AnalystAuthor Commented:
What I did is

 <?php
            // unset cookies
            if (isset($_SERVER['HTTP_COOKIE'])) {
                $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
                foreach($cookies as $cookie) {
                $parts = explode('=', $cookie);
                $name = trim($parts[0]);
                setcookie($name, '', time()-1000);
                setcookie($name, '', time()-1000, '/');
            }
           }
            
          ?>      

        <?php $random = rand(10,1000) ?>

        <iframe width='105%' height='600px' frameBorder='0' src='https://a.tiles.mapbox.com/v4/xxxxx.oxxxxxx/attribution.html?q=<?php echo($random) ?>&access_token=pk.eyJ1Ijoaisiauqw8q98w9d998xxxxxxxxxxxuywuqywuyqwuy'></iframe>
    	</div>

Open in new window


But the cache still isn't cleanned....
0
Eduardo FuerteDeveloper and AnalystAuthor Commented:
I guess it's the better solution since as I observate mapbox has a delay in actualize the maps.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.