Why cant i cache my PHP pages?

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
HeadchopperzAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
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
 
Ray PaseurCommented:
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
 
HeadchopperzAuthor Commented:
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
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
Ray PaseurCommented:
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
 
HeadchopperzAuthor Commented:
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
 
Ray PaseurCommented:
Is this true in all browsers?
0
 
HeadchopperzAuthor Commented:
ive oonly tried chrome and ie, but im going to give up trying to achieve this

Thanks
0
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
@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
 
HeadchopperzAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.