robindean Posted March 5, 2007 Share Posted March 5, 2007 I'm using one large php doc to store my entire line of content. It's only 40k total, but I'd like to cache the entire file for faster serving regardless. The way my site loads, it queries the ? string at the end of a url and prints based on the result. I'd like to cache the ENTIRE file so that when people surf, it feels "instantanious". Methods? Suggestions? Link to comment https://forums.phpfreaks.com/topic/41236-solved-cache-setting/ Share on other sites More sharing options...
Vikas Jayna Posted March 5, 2007 Share Posted March 5, 2007 Put the cache headers on the top of the file like this:- header("Cache-Control: public"); header("Expires: " . gmdate('D, d M Y H:i:s', time()+(3600*24)) . " GMT"); The above code will cache the file for 24 hours on the user's machine. But the file will have to be downloaded at least once and only subsequent requests will get served from the cache Link to comment https://forums.phpfreaks.com/topic/41236-solved-cache-setting/#findComment-199787 Share on other sites More sharing options...
robindean Posted March 5, 2007 Author Share Posted March 5, 2007 Does this mean that the file would download once (upon first visit) and then all new query strings would be loaded from the cached file? i.e. I'm using things like: pagetitle.php?display=1thing vs. pagetitle.php?display=anotherthing would differences in the ? string force the viewer to download the entire 40k file again or would it load from cache? Link to comment https://forums.phpfreaks.com/topic/41236-solved-cache-setting/#findComment-200183 Share on other sites More sharing options...
Azu Posted March 6, 2007 Share Posted March 6, 2007 PHP is processed on the server side, so for the PHP to output something different, the server needs to process it again and upload it to the client again. Meaning that every single page needs to be downloaded at least once before it can be displayed. And they don't have to download the entire php file, only the stuff that you send them with echo, print, die, etc.. Link to comment https://forums.phpfreaks.com/topic/41236-solved-cache-setting/#findComment-200622 Share on other sites More sharing options...
Vikas Jayna Posted March 6, 2007 Share Posted March 6, 2007 Yes! the differences in the querystring(?) will force the browser to download it again and rightly so - because if there is something different in the url, then the output could be different. Link to comment https://forums.phpfreaks.com/topic/41236-solved-cache-setting/#findComment-200629 Share on other sites More sharing options...
robindean Posted March 6, 2007 Author Share Posted March 6, 2007 Understood. Thanks guys. Link to comment https://forums.phpfreaks.com/topic/41236-solved-cache-setting/#findComment-201001 Share on other sites More sharing options...
object Posted March 6, 2007 Share Posted March 6, 2007 You can use output buffering to capture your php scripts output before it is sent to the browser, then store the output in a html file. Then change the links on your website to point to the html files instead of the php files. With some more coding, you can create one php script that will create cache files of your entire web directory. Of course this is all dependent on whether or not you host allows output buffering, and at what byte size. Use the following code as a guide. <? ob_start(); //starts output buffering include( $path_to_php_script ); // when a php script is included, it is first executed, so the include function returns the html output of the php file $html = ob_get_contents(); // gets the contents of the output buffer, which is your html, and stores them in $html ob_end_clean(); // ends output buffering, and does not output the buffer to browser $new_cache = fopen( $path_to_html_file, "w" ); // will create a new file, or open and overwrite it if it already exists flock( $new_cache, LOCK_EX ); // locks file to prevent errors when writing fwrite( $new_cache, $html ); // writes html from $html to file flock( $new_cache, LOCK_UN ); // unlocks file fclose( $new_cache ); // closes file ?> Link to comment https://forums.phpfreaks.com/topic/41236-solved-cache-setting/#findComment-201040 Share on other sites More sharing options...
Azu Posted March 7, 2007 Share Posted March 7, 2007 That wouldn't do any good. The client would still have to download those pages. They would just be pre-generated. Which is bad. It means they aren't dynamic, which means you shouldn't even be using PHP. Link to comment https://forums.phpfreaks.com/topic/41236-solved-cache-setting/#findComment-201602 Share on other sites More sharing options...
Jenk Posted March 7, 2007 Share Posted March 7, 2007 PHP caching over a 1 hour timeout: <?php $page = '/path/to/cache/page/file.html'; if (file_exists($page) && ((time() - filemtime($page)) < 3600)) { echo file_get_contents($page); } else { ob_start(); /** /* create the page here.. */ file_put_contents($page, ob_get_contents()); ob_end_flush(); } ?> Link to comment https://forums.phpfreaks.com/topic/41236-solved-cache-setting/#findComment-201605 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.