-4

I'm using fastcgi-cache full page caching and using php for geoip_country_code, I just used a vpn and connected to another country and I realized the page is being cached.

I followed this: https://www.howtoforge.com/using-geoip-with-nginx-on-ubuntu-12.04 but it's only php examples, and I can't find any javascript examples. I'm calling the variables from fastcgi_params, that's only accessible by php right?

My question is: How to stop this code from caching, I think javascript would not cache it then right?

<?php 
    $geoip_country_code = getenv(GEOIP_COUNTRY_CODE);
    $geoip_country_name = getenv(GEOIP_COUNTRY_NAME);
    switch ($geoip_country_code) {
      case "US":
        echo "USA";
        break;
      case "UK":
      case "IE":
      case "AU":
        echo "AU";
        break;
      default:
        echo "default";
    }

    ?>

If so how to make this php as javascript?

So far I have tried:

<?php 
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache"); // HTTP/1.0
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    $geoip_country_code = getenv(GEOIP_COUNTRY_CODE);
    $geoip_country_name = getenv(GEOIP_COUNTRY_NAME);
    //etc..

and it still caches the php code, which is why I'm asking how to do it in javascript?

So far I got:

<script type="text/javascript">
        document.write("Entering switch block<br />");
        var $geoip_country_code = getenv(GEOIP_COUNTRY_CODE);
        var $geoip_country_name = getenv(GEOIP_COUNTRY_NAME);
        switch ($geoip_country_code) {
          case "US":
            text = "USA";
            break;
          case "UK":
          case "IE": 
          case 'AU': document.write("Good job<br />");
            break;
          default:
            document.write("Good job<br />");
        }
    </script>

But it's not working, my javascript is limited, what am I doing wrong?

Hopelessone
  • 317
  • 1
  • 11

2 Answers2

0

It isn't necessary to use JavaScript. Use cache control in PHP (by adding a tag to the header to prevent caching):

How to prevent Browser cache for php site

Community
  • 1
  • 1
Jonathan Jeffrey
  • 288
  • 1
  • 10
  • all of this is not working: header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1 header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); // HTTP/1.0 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past – Hopelessone Jun 20 '16 at 02:37
  • @Hopelessone It might be the fastcgi-cache getting in your way. I'd recommend looking here: https://mattgadient.com/2013/12/20/flushing-the-nginx-fastcgi-cache-via-php-and-or-wordpress/ – Jonathan Jeffrey Jun 20 '16 at 02:45
  • Hi Jonathan, No that will purge the cache, I just want to run this piece of code in java instead of php to avoid the caching by fastcgi-cache.. – Hopelessone Jun 20 '16 at 02:48
0

It turns out that fastcgi cache is not caching the same page for everybody.

This PHP code therefore is not cached the same.

I thought it did. I ran this PHP GeoIp code that displays the user's country name, I flushed the cache and loaded the page, it said Australia, my friend loaded the page in Belarus and it said Belarus instead of Australia after fastcgi cached the page.

So fastcgi cache must be per user instead of caching the same page for all.

Hopelessone
  • 317
  • 1
  • 11