Reputation: 75
I'm trying to create a page that, when refreshed, will randomly load a url from a list of URLs. The best way I have found to do this so far is to have PHP grab the line randomly from the file and then load it into an iframe. This also allows me to have a close button on a top bar that allows whatever page was loaded into the iframe to breakout.
The problem that I am having is that in firefox after a couple reloads the iframe just starts reverting to a cache and won't load anything new. I'm guessing it's a cache issue because pressing Ctrl+F5 will make the iframe load a new page.
I've tried put a bunch of anti cache meta tags in as well as a peice of javascript that I found on this article.
So far nothing has worked. Does anyone know a good workaround or see something wrong in my code (I'm very much a novice).
Thanks for any help!
Here is the code:
</html>
<head>
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="expires" content="FRI, 13 APR 1999 01:00:00 GMT">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function Ionload()
{
$(parent.document).find("iframe").each(function() {
// apply the logic only to the current iframe only
if(this.contentDocument == window.document) {
// if the href of the iframe is not same as
// the value of src attribute then reload it
if(this.src != location.href) {
this.src = this.src;
}
}
});
}
</script>
<?php
class MyClass
{
function GetLine()
{
global $line;
srand ((double)microtime()*1000000);
$f_contents = file ("urlz");
$line = $f_contents[array_rand ($f_contents)];
}
function PrintVar()
{
global $line;
print $line;
}
}
MyClass::GetLine();
?>
<style type="text/css" media="all">
html, body {
height: 100%
}
body {
margin: 0;
overflow: hidden;
}
#topbar {
height: 50px;
width: 100%;
border-bottom: 3px solid #666
}
#page {
height: 100%;
width: 100%;
border-width: 0
}
</style>
</head>
<body>
<div id="topbar">
<a href=<?php MyClass::PrintVar();?> target="_top">close</a>
</div>
</body>
<iframe id="page" name="page" onload="Ionload()" src=<?php MyClass::PrintVar();?> frameborder="0" noresize="noresize"></iframe>
</html>
Update:
With some help from GGG I got it fixed. Here is the change to the function:
function GetLine()
{
global $newline;
srand ((double)microtime()*1000000);
$f_contents = file ("urlz");
$line = $f_contents[array_rand ($f_contents)];
$newline = $line . "?foo=" . rand();
}
I went with a random number instead of a sequence as I didn't know how to carry a sequence from one reload to another but this works.
I also noticed that the problem still exists if firefox is refreshed in less than two seconds after page load, but I can live with that.
Upvotes: 5
Views: 1030
Reputation: 76736
Try tacking a dummy query string onto the URL so the browser is forced to skip the cache.
For example, instead of loading www.google.com
, load www.google.com?foo=N
where N
is a number that you increment with each load.
Upvotes: 3