Skip to content

Instantly share code, notes, and snippets.

@CitizenOfRome
Created August 27, 2014 18:07
Show Gist options
  • Select an option

  • Save CitizenOfRome/e36ee64d5b1bf9a83339 to your computer and use it in GitHub Desktop.

Select an option

Save CitizenOfRome/e36ee64d5b1bf9a83339 to your computer and use it in GitHub Desktop.
Clear cache and cookies if they are from an older version
<?php
define('APP_VERSION', '1.2.14');
if ($_COOKIE['APP_VERSION'] !== APP_VERSION) {
// Delete all cookies and invalidate cache if it is from an older version
if (isset($_SERVER['HTTP_COOKIE'])) {
// Unset cookies - http://stackoverflow.com/a/2310591/937891
$cookies = explode(';', $_SERVER['HTTP_COOKIE']);
foreach ($cookies as $cookie) {
$parts = explode('=', $cookie);
$name = trim($parts[0]);
setcookie($name, '', time() - 1000);
setcookie($name, '', time() - 1000, '/');
}
}
// Uncache current page - http://stackoverflow.com/a/11057772/937891
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
$_COOKIE['APP_VERSION'] = APP_VERSION;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment