PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Nov 21, 2017 4:59 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 34 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: Fri Oct 07, 2016 5:40 am 
Offline
Forum Contributor

Joined: Fri Jul 18, 2014 1:54 pm
Posts: 170
Hi, This code does not work consistently. Some background is in order on this one. I am running my website on my local host using xampp then I pull up pages and save them to my downloads directory using google chrome and then move them up to the web hosting directory. I am doing this because there is no money being made and at present things are really simple so I don't want a VPS at the webs hosting company. So I discovered that often the updates were not getting picked up because google chrome puts things in cache memory. This was happening both on the local host and on the webhost. So at first I put a comment on the pages telling the 20 people using these static pages to press F5. Then I realized there should be a way to automate it. I found the javascript below. It worked except I wanted to avoid the double running of the code if possible (avoid the screen flash too). So I asked on another forum that is part of my program that gives me WYSIWYG assistance and the main owner of that company suggested the html code below. It did not work at all from what I could tell. So I put the javascript code back in. Then I noticed with the debugger last night that the javascript code reruns the php code every single time without fail (runs it twice). So then I put in the local host test in to bypass the 2nd run if the local host was producing the pages and at first that seemed to fix the problem (the static pages did run the javascript code to ensure the users got the latest data but the xampp local host code did not run the php twice but did at least run it once). So this morning I found that this failed and the updates were not showing. So I decided to comment the localhost test out of the javascript and let it force the php to run twice to be sure the updates are reflected on the page every time. However this seems kind of dump to me. Surely there must be a proper solutions that forces google chrome to never store a specific page in cache memory such that the page does the php run but only once. Maybe someone has struggled with this before and found a solution that actually works (a solution that is 100% guaranteed to shut off the caching of a page forcing the localhost php code to build the page once only every time or causing the static pages to always be picked up from the webhost computer).

Thanks,
John

Syntax: [ Download ] [ Hide ]
<!-- START: HTML code to make sure your page is not stored in cache so that you do not need to tell them to use F5 -->
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
<!-- END: HTML code to make sure your page is not stored in cache so that you do not need to tell them to use F5 -->

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" />

<script>
function funcOnloadProcessing() {


   //START: code to bypass the F5 refresh if we are working from the local host. The static pages still do an F5 refresh
   // IMPORTANT: It seems that you still have to force this even if you are on the local host. What good is that because most times everything runs twice.
   //var MyHost = window.location.hostname;
   //var MyPath = window.location.pathname;
   //if(MyHost == 'localhost') {
   //   return true;
   //}
   //END: code to bypass the F5 refresh if we are working from the local host.  The static pages still do an F5 refresh

   //START: code to force an F5 refresh
   $(document).ready(function(){    
       //Check if the current URL contains '#'
       if(document.URL.indexOf("#")==-1){
           // Set the URL to whatever it was plus "#".
           url = document.URL+"#";
           location = "#";
   
           //Reload the page
           location.reload(true);
       }
   });
   //END: code to force an F5 refresh

}
</script>

<noscript>
<p>
This website will not run properly without javascript enabled. Please enable javascript and try again.<br>
This website will not run properly without javascript enabled. Please enable javascript and try again.<br>
This website will not run properly without javascript enabled. Please enable javascript and try again.<br>
This website will not run properly without javascript enabled. Please enable javascript and try again.<br>
This website will not run properly without javascript enabled. Please enable javascript and try again.<br>
This website will not run properly without javascript enabled. Please enable javascript and try again.<br>
This website will not run properly without javascript enabled. Please enable javascript and try again.<br>
This website will not run properly without javascript enabled. Please enable javascript and try again.<br>
</p>
</noscript>
 


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 6:27 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6581
Location: WA, USA
Hold on a second here.

It sounds like you're saying that
1. You're writing stuff involving PHP code.
2. When you're satisfied with your stuff, you use Chrome to save the page as displayed by XAMPP, then upload that file you just "downloaded" to your host?


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 10:16 am 
Offline
Forum Contributor

Joined: Fri Jul 18, 2014 1:54 pm
Posts: 170
Regarding what I am doing, I get an email from a very small number of people to do an update on my local host, I do the update on my localhost, I display the pages I want to show on the webhosting site, I save them in google chrome by doing this (right click, save as, webpage complete), then I put the pages on the webhost machine where they can see the updates. If I get too busy and I start making enough money to pay for the VPS then I put the software on a VPS and I have them do the updates.

Regarding the question I have, I came up with a simple google search "how to stop web page caching". If found the page below.
http://www.htmlgoodies.com/beyond/refer ... he-Huh.htm
He mentioned two of the commands I was using the first time around and explained why they do not work.
He seems to have researched it fairly well so I am going to try this for a few days to see if it ever fails.

I also found the chrome://cache/ command to see what pages are in google chrome cache and I found this webpage which explains how to clear cache without clearing other stuff.http://superuser.com/questions/278948/c ... -in-chrome
By doing this I can more quickly determine if the change worked rather than waiting a few days.


Also this worked well to clear the cache of the pages for my webiste so I can see if they are getting in cache now by using the above mentioned chrome://cache/ command
Http://superuser.com/questions/278948/c ... -in-chrome
Open up a page on the website you want to clear the pages for.
in google chrome just left of the URL box click on the "reload page" button but hold it until you see a little menu of three or so items.
From the popup list, choose "Empty Cache and Hard Reload".
Not sure this clears the cache for the entire domain. But probably satisfies 90% of the use cases of people that visit this SE question.
Note: In some cases this will NOT open the popup list. This is because the page isn't cached at that time, so the menu is not invoked.


These instructions from Google actually worked for clearing the pages from google chrome cache so I can test the new html code:
Open Chrome.
On your browser toolbar, click More More.
Point to More tools, and then click Clear browsing data.
In the "Clear browsing data" box, click the checkboxes for Cookies and other site and plug-in data and Cached images and files.
Use the menu at the top to select the amount of data that you want to delete. Choose beginning of time to delete everything.
Click Clear browsing data.

This has a lot of info.
http://stackoverflow.com/questions/4954 ... l-browsers


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 1:09 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6581
Location: WA, USA
Slow down a second. I need to confirm one more thing:

Are you using PHP code to generate the contents of the page? Is it that generated content which never changes, and why you think it's the browser at fault?

A simple yes or no is sufficient.


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 1:59 pm 
Offline
Forum Contributor

Joined: Fri Jul 18, 2014 1:54 pm
Posts: 170
Yes, I am using PHP to generate the page. The PHP code is well tested and working. No problems there. I use netbeans to make sure it is working properly.

I am not good at short answers...lol. Typical programmer :-)

My latest and greatest attempt to keep the page from every being displayed from cache memory is below. I have it twice (once between the <head></head> tags and once at the end of the page as per the first website suggestion). I am trying to figure out a fast way of knowing for sure if the page is in cache memory after it has been displayed on the screen. I have just now been using the chrome developer tools (F12) network to try and figure it out. On that developer tool if I click "disable cache" it appears to not be using cache. But if that checkbox is not pressed and I start fresh to load the page I get this: "Status Code:200 OK (from cache)". So maybe that means the code below is not actually working????

Syntax: [ Download ] [ Hide ]
<!-- START: HTML code to make sure your page is not stored in cache so that you do not need to tell them to use F5 -->
<!-- http://www.htmlgoodies.com/beyond/refer ... he-Huh.htm -->
<!-- http://stackoverflow.com/questions/4954 ... l-browsers -->
<HEAD>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="expires" content="-1" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
</HEAD>
<!-- END: HTML code to make sure your page is not stored in cache so that you do not need to tell them to use F5 -->
 


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 2:12 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6581
Location: WA, USA
(where's the right emoticon for this... ah)

:banghead:

The problem has nothing to do with Chrome. It has nothing to do with caching. Congratulations on becoming familiar with a subject that is often an issue for people, but this time it is not to blame.

When you wrote your code you put it into a .php file and accessed it through your browser at http://localhost/file.php or something similar. Apache receives the request, tells PHP to execute that file, and the output gets sent back to your browser. You don't see the code itself but the result of the code executing. The HTML it generated and whatever.

When you saved in Chrome, you saved that result. You saved a copy of the page that PHP generated that one time you accessed it. You don't get any of the code that was involved in the process.

Take a look for yourself: compare that file you saved with the stuff you wrote. None of the PHP code will be there.

You have to upload the original file. What you wrote. And you need a host that supports PHP but I can only assume yours does that (or else you wouldn't be using them). That way when people access the page the PHP runs for itself and does whatever it needs to do.

While you're in there, remove all the caching stuff you've added. You very likely don't need it, and in fact it could be harmful.


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 2:14 pm 
Offline
Forum Contributor

Joined: Fri Jul 18, 2014 1:54 pm
Posts: 170
I think maybe I found the problem. The code above (which was suggested by the earlier mentioned google search link) I had incorrectly placed below the </HTML> tag rather than just above it. So the way I know it appears to be working is now when I load the page fresh while using the chrome developer tools is I no longer get "Status Code:200 OK (from cache)" but rather I get "Status Code:200 OK". This is the same message I get if I check the checkbox in the developer tools to disable the cache.


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 2:31 pm 
Offline
Forum Contributor

Joined: Fri Jul 18, 2014 1:54 pm
Posts: 170
Yes, I understand what you are saying totally except what you are saying in the quote below is wrong.

Quote:
You have to upload the original file. What you wrote. And you need a host that supports PHP but I can only assume yours does that (or else you wouldn't be using them). That way when people access the page the PHP runs for itself and does whatever it needs to do.


Just to let you know that I know what I am talking about here, Again, I run xampp on my local host which processes the php code and ships the html code and the javascript up to the browser on my local host and it displays it. I often right click on the resulting page and click "view page source" and as you say I see the html code without any of the php code that is generating it. Okays so try this yourself. Generate a page on your local host and right click on the page and instead of clicking "view source" instead click "save as" then save it to some work area on your C drive. Then go to that place and double click on the file name you just saved and it will display the static page. It works fine. I then ship this static page up to the webhosting machine and it works fine. I have friends who view it from their computers in a different city and they tell me it looks fine.

I had a VPS about two years ago and I was running php code on the webhosting service. It was costing me $20.00 a month. By doing it this way using static web pages (a much simpler website) I only need to pay $4.00 a month. Again, once my project proves itself and I get more than 20 people sending me requests to update the file then I will do what you are saying by putting my php code on the VPS service at the webhosting company and off load the work to them with a login process.

I just tried again and I still get that message "Status Code:200 OK (from cache)" so I guess it is still reloading my page from cache?


Last edited by bowlesj on Fri Oct 07, 2016 2:44 pm, edited 2 times in total.

Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 2:39 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6581
Location: WA, USA
If you've solved it then... great!

So you're only using PHP to, like, generate the page you want to show? You don't need PHP actually running on the live site, just static HTML and CSS/Javascript?


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 2:42 pm 
Offline
Forum Contributor

Joined: Fri Jul 18, 2014 1:54 pm
Posts: 170
Quote:
So you're only using PHP to, like, generate the page you want to show? You don't need PHP actually running on the live site, just static HTML and CSS/Javascript?


Yes that is exactly correct.

Ironically by doing it with static pages I was much more likely to learn about this cache issue because I know what the webhosting page was displaying before and I know what the updated page is suppose to look like and I realize that my updates are not showing then once I press F5 my updates do show. In other words, it slowed the whole process down enough that I caught on.

Anyway, I updated my prior post. I am not sure if I have solved it. The guy in this link seems to really know a lot about the caching issue.
http://stackoverflow.com/questions/4954 ... l-browsers
He seems to know how these header commands are processed in great detail. I read that the order is important. Maybe I have them sequenced incorrectly. But like I say, I am not sure. Maybe it is working.

UPDATE: I think I have some of the code positioned incorrectly relative to the <html> commands and also incorrectly relative to the <head> commands too. Fixing it right now.


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 3:42 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6581
Location: WA, USA
If you think you have everything set up right but it's still not working, then

You should try to identify the exact source of the problem before embarking on a mission to try to fix everything that could possibly be at fault.
Can you reproduce the issue yourself? Can you verify that the browser is not, in fact, re-requesting the page every time? (Inspector thing, Network tab, load page, and the request should say 200 without being served from the cache)


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 4:38 pm 
Offline
Forum Contributor

Joined: Fri Jul 18, 2014 1:54 pm
Posts: 170
yes, slow it down and dissect it.

This is a good link.
http://stackoverflow.com/questions/1341 ... l-browsers
One person recommends this case study link. I agree. It is very good.
http://securityevaluators.com/knowledge ... s/caching/

In this example one person has a comment I copied below.
Syntax: [ Download ] [ Hide ]
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
 

Quote:
The list is just examples of different techniques, it's not for direct insertion. If copied, the second would overwrite the first and the fourth would overwrite the third because of the http-equiv declarations AND fail with the W3C validator. At most, one could have one of each http-equiv declarations; pragma, cache-control and expires.


So after reading a bit of them all (as a simplification) it appears that I should just try "no-store" since apparently Chrome only needs this (I think....lol).

That problem is I can't figure out how to confirm it. The F12 thing and the chrome://Cache command both say it is not working.


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 5:02 pm 
Offline
Forum Contributor

Joined: Fri Jul 18, 2014 1:54 pm
Posts: 170
Now this is shedding some light on what is going on. I am storing it here just in case I lose it.
http://www.benhallbenhall.com/2012/03/h ... cache-304/

Here is a quote.
Quote:
200 (from cache) vs 304

Now the other day while performing a site performance audit I noticed that a lot of our assets were returning 304 statuses. While comparing another site I noticed that it was returning a 200 (from cache) status code. This intrigued me and I wanted to dig deeper.

It turns out that when a 200 (from cache) response is given it means that a future expiration date on the content is set. In essence the browser doesn’t even really communicate with the server to check on the file. It knows not to do it until the expiration date has expired.

By contrast a 304 goes to the server and receives a response back that the data has not change. The server is telling the browser to use the cache as a result.


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 5:38 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6581
Location: WA, USA
Caching is very easy to address if you can determine the exact problem. Stop taking the shotgun approach and throwing all sorts of markup at it, hoping that one of them will make the problem go away, and determine precisely what is going on.

How is the browser requesting the page? 200? From cache? 304? What are the actual response headers from the server?


Top
 Profile  
 
PostPosted: Fri Oct 07, 2016 5:50 pm 
Offline
Forum Contributor

Joined: Fri Jul 18, 2014 1:54 pm
Posts: 170
I am getting a mix of 304 and 200.

Quote:
What are the actual response headers from the server?

Regarding your question, I am assuming you mean "can you see the actual meta command I am sending when I view the F12 network info". That is the problem. I am not sure if my meta commands are being used since I see no reference to them. This is one of my last ones. I assume it means it got the data from cache which is what I don't want.

Quote:
Request Method:GET
Status Code:200 OK (from cache)
Remote Address:23.111.72.10:80
Response Headers
Accept-Ranges:bytes
Content-Length:51791
Content-Type:text/html
Date:Fri, 07 Oct 2016 22:44:44 GMT
Last-Modified:Fri, 07 Oct 2016 21:48:05 GMT
Server:Apache
Request Headers
Provisional headers are shown
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 34 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group