I have a PHP page that displays differently for each person viewing it. I want to be able to cache the HTML of the PHP page in the user's browser so that when they go back to that page, it will load the HTML version instead of running through all the PHP processes again.
The thing is, I want to be able to control whether or not the cached version will be displayed through $_GET variables appended to the URL. For example, if I went to http://www.mysite.com/page.php?cache=true, then it would display the cached version. If I went to http://www.mysite.com/page.php?cache=false, then it would run through all the PHP processes. How would I go about doing this?
Caching dynamic content in the user's browser
Moderator: General Moderators
Re: Caching dynamic content in the user's browser
Are your users logged in? In other words, does each user have a unique string that can be used as a file name?
You can use PHP's output buffering functions to capture the generated page content so you can save it to a file.
PHP Manual: Output Buffering Control
Edit: This post was recovered from search engine cache.
You can use PHP's output buffering functions to capture the generated page content so you can save it to a file.
PHP Manual: Output Buffering Control
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 1:05 pm, edited 1 time in total.
Re: Caching dynamic content in the user's browser
Yes that is definitely a possibility, but there will be thousands of users and I am not sure that if it would be a good practice to load the server with thousands of files. I have multiple PHP pages that I want to cache, and I think it would just be overkill to create a separate file for each user. Is there a more client-side way of doing it?
Re: Caching dynamic content in the user's browser
If you're caching the files, you have to store them somewhere. That means they are either taking up space on the filesystem or in a database.
If you store the cache on the filesystem, you can separate the users into groups (by the first letter of their name or something) and have a directory for each group. Inside each directory, have a directory for each user that contains all of a user's cached pages.
You can cache them on the client side using cookies as long as the size of the pages is not excessive. *cringe* Use Javascript to access the cookies because if you use PHP it could result in twice the amount of traffic (download + upload). I'm not sure about this because I have never tried it. Maybe I will, just for fun.
Edit: This post was recovered from search engine cache.
If you store the cache on the filesystem, you can separate the users into groups (by the first letter of their name or something) and have a directory for each group. Inside each directory, have a directory for each user that contains all of a user's cached pages.
You can cache them on the client side using cookies as long as the size of the pages is not excessive. *cringe* Use Javascript to access the cookies because if you use PHP it could result in twice the amount of traffic (download + upload). I'm not sure about this because I have never tried it. Maybe I will, just for fun.
Edit: This post was recovered from search engine cache.