?
Solved

Why cant i cache my PHP pages?

Posted on 2012-09-11
10
Medium Priority
?
554 Views
Last Modified: 2012-09-12
Ok, this is driving me nuts.
Ill explain what i want and why i need it.
I have a homepage that is only updated once a day, bit this page has a lot of resources, and takes the longest out of all my pages.
So, seeing as the content is static for a day theres no reason not to cache it...


But it wont cache

The page is using HTTPS, and the resoucee on the page (images/js/css) all cache fine. But the html output simply, doesnt cache.

I have more information here, i made with caffinated rage that has more info: http://stackoverflow.com/questions/12361402/no-matter-what-i-do-i-cannot-cache-my-dynamic-php-files

It needs to be done in php and not in the htaccess, im using a VDS, so i can change everything
0
Comment
Question by:Headchopperz
  • 6
  • 4
10 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 38386623
Please see these links.  Visit them in order.
http://www.laprbass.com/RAY_dynamic_to_static_page.html
http://www.laprbass.com/RAY_dynamic_to_static_page.php
http://www.laprbass.com/RAY_dynamic_to_static_page.html

Here is the code:

<?php // RAY_dynamic_to_static_page.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

// CREATE A VARIABLE FOR OUR HTML
$xyz = 'Hello World on ' . date('c');

// CREATE OUR WEB PAGE IN HTML5 FORMAT
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<title>HTML5 Page</title>
</head>
<body>
<p>$xyz</p>
</body>
</html>
HTML5;

// CONFIGURE AN HTML URL
$uri = $_SERVER['REQUEST_URI'];

// STRIP LEADING SLASH AND CHANGE SUFFIX
$uri = preg_replace('#^/#', NULL, $uri);
$uri = preg_replace('#\.php#i', '.html', $uri);

// USE OUTPUT BUFFERS TO CAPTURE THE WEB PAGE
ob_start();
echo $htm;
$htm = ob_get_contents();

// WRITE THE STATIC VERSION OF THE PAGE
file_put_contents($uri, $htm);

// AT END OF SCRIPT, PHP WILL RENDER THE WEB PAGE TO THE BROWSER

Open in new window

By way of explanation, here is what goes on in this script.  First, the HTML string is created by PHP.  In this case, the only variable is the timestamp, but this HTML document could be as big and complex as you might want (there is no limit to the length of a string variable in PHP).  Next, the script uses output buffering to capture the browser output, then it writes the browser output.  Finally, it writes the captured output into a ".html" document on the server.  When the script ends, the captured output buffer is automatically flushed by PHP.

In the aggregate this has the effect that if you visit the PHP version of this page, you will cause the server to regenerate the static HTML page.  And, obviously, if the client visits the HTML version, she will see the most recently created static HTML version, and will not run the PHP scripts.

If your server looks first for the .HTML version of your home page, and then looks for the .PHP version of your home page, this strategy should work well.  In order to regenerate the HTML document, just delete it.  The next access to the home page will not find the HTML document and will cause the server to look for the PHP script.

Does that make sense for your needs?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38386695
Hmm... This example seems to show better consolidation of thought...
<?php // RAY_dynamic_to_static_page.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

// CREATE DYNAMIC VARIABLES FOR OUR STATIC HTML DOCUMENT
$xyz = 'Hello World on ' . date('c');
$get = isset($_GET['q']) ? $_GET['q'] : NULL;
$get = htmlentities($get);

// CREATE OUR WEB PAGE IN HTML5 FORMAT
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<title>HTML5 Dynamic/Static Page</title>
</head>
<body>
<p>$xyz</p>
<p>$get</p>
</body>
</html>
HTML5;

// CONFIGURE A STATIC HTML URL (WITHOUT THE GET ARGS)
$uri = $_SERVER['PHP_SELF'];

// STRIP LEADING SLASH AND CHANGE SUFFIX
$uri = preg_replace('#^/#', NULL, $uri);
$uri = preg_replace('#\.php$#i', '.html', $uri);

// SAVE THE STATIC VERSION OF THE PAGE
file_put_contents($uri, $htm);

// WRITE THE WEB PAGE TO THE BROWSER
echo $htm;

Open in new window

0
 

Author Comment

by:Headchopperz
ID: 38388055
Does that make sense for your needs?

It caches it on the server as shown. Now your version on your site, also caches it on my PC. but when i do it, it wont cache on my pc.. So each time i gotta reload the page.

Is it HTTPS causing it? I cant imagine so because why would they let me cache JS and not HTML?

One thing worth noting, yours sends out the header which contains the files size, mine does not, is this it?

Even putting: header("Content-Length: " . strlen($htm));

Wont make it send Content-Length... Is this a glitch?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38388276
Not sure about the Content-length header, but that is certainly something worth considering.  I am not sure I can think of any reason why the header() command would be ignored.  Do you have error_reporting(E_ALL) and "log errors" set on?  If so there may be a clue in the error logs.
0
 

Author Comment

by:Headchopperz
ID: 38388328
look here: https://188.121.51.0/Ex.php (it will say cert errors, ignore that).

It will show you an image which is being cached fine.

it also has an attached jQuery file, (JQuery UI), which is caching fine.

Neither HTML or PHP page cache...


I literally turned off everything related to caching in the htaccess.

<?php // RAY_dynamic_to_static_page.php
error_reporting(E_ALL);
date_default_timezone_set('Europe/Jersey');

// CREATE DYNAMIC VARIABLES FOR OUR STATIC HTML DOCUMENT
$xyz = 'Hello World on ' . date('c');
$get = isset($_GET['q']) ? $_GET['q'] : NULL;
$get = htmlentities($get);

// CREATE OUR WEB PAGE IN HTML5 FORMAT
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<title>HTML5 Dynamic/Static Page</title>
</head>
<body>
<p>$xyz</p>
<p>$get</p>
    <br/>
    <a href='Ex.php'>PHP File Reload</a> -  <a href='Ex.html'>HTML File Reload</a>
    <br/>
    Example Cached Image: <img src='/Images/board2.png' alt=' ' />
    
    <script type='text/javascript' src='/Include/JS/JQueryUI.min.js'></script>
 
    </body></html>
HTML5;

// CONFIGURE A STATIC HTML URL (WITHOUT THE GET ARGS)
$uri = $_SERVER['PHP_SELF'];
/*
header("Pragma: public");
header("Cache-Control: max-age=604800");
$date = date("Y-m-d");
$date = date("D, d M Y", strtotime(date("d M Y", strtotime($date)) . " +1 day"));
header('Expires: ' . $date . ' 00:00:01 GMT');*/


// STRIP LEADING SLASH AND CHANGE SUFFIX
$uri = preg_replace('#^/#', NULL, $uri);
$uri = preg_replace('#\.php$#i', '.html', $uri);

// SAVE THE STATIC VERSION OF THE PAGE
file_put_contents($uri, $htm);


// WRITE THE WEB PAGE TO THE BROWSER
echo $htm;

Open in new window


I turned off the HTTPS requirement, and it doesnt work even under standard Port 80:

http://188.121.51.0/Ex.php

[edit]

It seems to work most of the time under HTTP actually, but not HTTPS...

That doesnt make sense to me because if you can control the Javascript, you can control the DOM anyway? So why protect the HTML files but not JS?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38388581
Is this true in all browsers?
0
 

Author Comment

by:Headchopperz
ID: 38388713
ive oonly tried chrome and ie, but im going to give up trying to achieve this

Thanks
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38388757
Yeah, I'm not getting it either.  Anyway, if you create the static version instead of running all the queries, it should help some.  Best, ~Ray
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38389550
@Headchopperz: Did your caffeinated rage get control of your EE grading?  When you return to EE, please come back with a sense of the grading guidelines.  The other experts will be looking forward to your participation.
http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26

Just because you asked a question with no real context and no good answer is not a sufficient reason to give the worst possible grade anyone can give at EE.  

Sorry if my efforts to help disappointed you.  Best of luck with your project, ~Ray
0
 

Author Comment

by:Headchopperz
ID: 38390359
Sorry, i wanted to give you points for helping, but i didnt think it would be fair on the system for giving you the full score because it wasnt resolved.
I didnt think 500 was unreasonable...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question