Reputation: 11
I'm using a standard php cache script on page.php
$cache = 'the_location/'.$id.'.html';
$expire = time() -3600 ;
if(file_exists($cache) && filemtime($cache) > $expire)
{
readfile($cache);
} else {
ob_start();
// some stuff
$pages = ob_get_contents();
ob_end_flush();
$fd = fopen("$cache", "w");
if ($fd) {
fwrite($fd,$pages);
fclose($fd);
}
echo $pages ; }
On main_page.php I'm loading page.php like so:
$('#div').load('page.php?id=' + id);
If I go straight to page.php?id=1234 the page is cached and file 1234.html appears in 'the_location' Otherwise on main_page.php nothing happens ...
Help is much appreciated !
Edit : Everything works on main_page.php and page.php, page.php is correctly loaded into main_page.php but not cached, if I load page.php through browser it is cached.
Upvotes: 1
Views: 454
Reputation: 148
I simulated this as basically as I could:
main_page.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head id="ctl00_Head1">
<script src="jquery-1.5.min.js"></script>
<script type="text/javascript">
var id = <?=$_GET['id']?>;
$(function(){
$('#div').load('page.php?id=' + id);
});
</script>
</head>
<body>
<div id="div">a</div>
</body>
</html>
page.php
<?php
$id = $_GET['id'];
$cache = 'the_location/'.$id.'.html';
$expire = time() -3600 ;
if(file_exists($cache) && filemtime($cache) > $expire)
{
readfile($cache);
} else {
ob_start();
echo 'This is a generated page';
$pages = ob_get_contents();
ob_end_clean();
$fd = fopen("$cache", "w");
if ($fd) {
fwrite($fd,$pages);
fclose($fd);
}
echo $pages;
}
This works for me. Things I noted while working through the code.
id
to php's $_GET['id']
in javascript in the main_page.php$id = $_GET['id'];
on page.phpob_end_flush();
along with echo $pages
is repetitive (content gets flushed, then shows up twice when the page is regenerated), use ob_end_clean()
or $pages = ob_get_flush()
.Upvotes: 1
Reputation: 8679
Have you looked at the request that is being sent using firebug? JQuery is probably appending a cache-busting GET variable to the URL hence busting your cache :D
Upvotes: 0