9

Here's a challenge that I was tasked with recently. I still haven't figured out the best way to do it, maybe someone else has an idea.

Using PHP and/or HTML, create a page that cycles through any number of other pages at a given interval.

For instance, we would load this page and it would take us to google for 20 seconds, then on to yahoo for 10 seconds, then on to stackoverflow for 180 seconds and so on an so forth.

GreenO
  • 101
  • 1
  • 1
  • 4

12 Answers12

19
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Dashboard Example</title>
<style type="text/css">
body, html { margin: 0; padding: 0; width: 100%; height: 100%; overflow: hidden; }
iframe { border: none; }
</style>
<script type="text/javascript">
var Dash = {
    nextIndex: 0,

    dashboards: [
        {url: "http://www.google.com", time: 5},
        {url: "http://www.yahoo.com", time: 10},
        {url: "http://www.stackoverflow.com", time: 15}
    ],

    display: function()
    {
        var dashboard = Dash.dashboards[Dash.nextIndex];
        frames["displayArea"].location.href = dashboard.url;
        Dash.nextIndex = (Dash.nextIndex + 1) % Dash.dashboards.length;
        setTimeout(Dash.display, dashboard.time * 1000);
    }
};

window.onload = Dash.display;
</script>
</head>
<body>
<iframe name="displayArea" width="100%" height="100%"></iframe>
</body>
</html>
Jonny Buchanan
  • 61,926
  • 17
  • 143
  • 150
  • there's a small error -- frames["displayArea"].location.href = dashboard.url; should read frames["displayArea"].location.href = dashboards.url; – Pete Michaud Sep 17 '08 at 17:41
  • I've tried this code above it works well - but one particular site seems to cause a refresh of the whole page (websitepulse) so the looping stops. Any way to prevent the site in the iframe taking over the whole browser page? – Codek Apr 06 '11 at 09:47
  • 1
    You could try using a Framekiller Killer: http://en.wikipedia.org/wiki/Framekiller#Framekiller_Killers – Jonny Buchanan Apr 06 '11 at 10:24
  • Perfect thanks. In the end I used this one because It was easier than setting up a http page to return code 204: http://framekiller.heroku.com/onbeforeunload – Codek Apr 06 '11 at 15:22
  • Awesome answer here. Would love to know how to insert this into frame of a website, that sites next to a second frame that loads different links. Like if you had pictures in one frame, and description in another. Very new to html. – Dan Ciborowski - MSFT Sep 29 '12 at 06:13
  • This likely won't work anymore because of stricter security policies. Try it -- you'll get a bunch of "Refused to display '...' in a frame because it set 'X-Frame-Options' to 'DENY/SAMEORIGIN'" errors. – thdoan Jul 23 '15 at 02:35
1

Use a separate iframe for the content, then use Javascript to delay() a period of time and set the iframe's location property.

Jason Cohen
  • 81,399
  • 26
  • 107
  • 114
1

When you are taken to another site (e.g. Google) control passes to that site, so in order for your script to keep running, you'd need to load the new site in a frame, and keep your script (which I'd imagine could most readily be implemented using Javascript) in another frame (which could be made very small so you can't see it).

Ben
  • 66,838
  • 37
  • 84
  • 108
1

I managed to create this thing. It's not pretty but it does work.

<?php
# Path the config file, full or relative.
$configfile="config.conf"; 
$tempfile="tmp.html";
# Read the file into an array
$farray=file($configfile);  
# Count array elements
$count=count($farray);  
if(!isset($_GET['s'])){
    $s=0;
}else{  
    $s=$_GET['s'];
if($s==($count-1)){ # -1 because of the offset in starting our loop at 0 instead of 1
    $s=0;
}else{
    $s=$_GET['s']+1; # Increment the counter
}
}
# Get the line from the array
$entry=$farray[$s];
# Break the line on the comma into 2 entries
$arr=explode(",",$entry);       
# Now each line is in 2 pieces - URL and TimeDelay
$url=strtolower($arr[0]);
# Check our url to see if it has an HTTP prepended, if it doesn't, give it one.
$check=strstr($url,"http://"); 
if($check==FALSE){
    $url="http://".$url;
    }           
# Trim unwanted crap from the time
$time=rtrim($arr[1]);               
# Get a handle to the temp file
$tmphandle=fopen($tempfile,"w");
# What does our meta refresh look like?
$meta="<meta http-equiv=\"refresh\" content=\"".$time.";url=index.php?s=".$s."\">\n";
# The iframe to display
$content="<iframe src =\"".$url."\" height=\"100%\" width=\"100%\"></iframe>";
# roll up the meta and content to be written
$str=$meta.$content;
# Write it
fwrite($tmphandle,$str);
# Close the handle
fclose($tmphandle);
# Load the page
die(header("Location:tmp.html"));            
?>

Config files looks like (URL, Time to stay on that page): google.com,5 http://yahoo.com,10

GreenO
  • 101
  • 1
  • 1
  • 4
0

You could do this with JavaScript quite easily. It would help to know the deployment environment. Is it a kiosk or something?

For the JavaScript solution, serve up a page that contains a JavaScript that will pop open a new browser window. The controller page will then cause the new browser window to cycle through a series of pages. That's about the simplest way to do this that I can think of.

Edit: Agree with Simon's comment. This solution would work best in a kiosk or large, public display environment where the pages are just being shown without any user interaction.

shadit
  • 2,536
  • 1
  • 16
  • 21
0

Depends on your exact requirements. If you allow JavaScript and allow frames then you can stick a hidden frame within a frameset on your page into which you load some JavaScript. This JavaScript will then control the content of the main frame using the window.location object and setTimeout function.

The downside would be that the user's address bar would not update with the new URL. I'm not sure how this would achievable otherwise. If you can clarify the constraints I can provide more help.

Edit - Shad's suggestion is a possibility although unless the user triggers the action the browser may block the popup. Again you'd have to clarify whether a popup is allowable.

0

Create a wrapper HTML page with an IFrame in it, sized at 100% x 100%. Then add in some javascript that changes the src of the IFrame between set intervals.

hasseg
  • 6,787
  • 37
  • 41
0

I think it would have to work like gabbly.com, which sucks in other websites and displays them with its own content over it.

Once you read the other site in and were ready to display it, you couldn't really do it "in PHP"; you would have to send an HTML redirect meta-tag:

<meta HTTP-EQUIV="REFRESH" content="15; url=http://www.thepagecycler.com/nextpage.html">

Or you could use Javascript instead of the meta-tag.

Max Cantor
  • 8,229
  • 7
  • 45
  • 59
0

This is not doable in a PHP script, unless you want to edit the redirect.... PHP is a back end technology; you're going to need to do this in Javascript or the like.

The best you're going to do, as far as I know, is to create a text file on your web server and load a different HTTP address based on time out of that text file, then redirect the browser to the site found in that text file.

0

The first solution that jumps to mind is to do this in a frameset. Hide one of the frames, and the other display the pages in question. Drive the page transitions with Javascript from the hidden frame.

function RefreshFrame()
{
    parent.VisibleFrame.location.href = urlArray[i];
    i++;

    if(i < urlArray.length) SetTimeout("RefreshFrame()", 20000);
}

var i = 0;
var urlArray = ['http://google.com','http://yahoo.com', 'http://www.search.com'];
RefreshFrame();

In this example the Javascript would be in the hiddend frame, and you would name your visible frame "VisibleFrame".

Disclaimer: I just wrote this code in the comment window and have not tested it

Jason Jackson
  • 17,016
  • 8
  • 49
  • 74
0

The theory behind the request is basically the ability to cycle through web page dashboards for various systems from a "kiosk" PC. I oversee a data center and we have several monitor systems that allow me view dashboards for temps, system up time, etc etc. The idea is load a page that would cycle from dashboard to dashboard remaining on each for an amount of time specified by me, 1 minute on this board, 30 seconds on the next board, 2 minutes on the next and so on.. Javascript is absolutely allowable (though I have little experience with it). My mediums of choice are PHP/HTML and I'm not seeing a way to make this happen cleanly with just them..

GreenO
  • 101
  • 1
  • 1
  • 4
-1

There's a bunch of ways you can do this, iv written several scripts and tools with everything from JS to Ruby

In the end It was much easier to use http://dashboardrotator.com . It handled browser restarts, memory allocation and accidental window closure for me with a nice simple GUI.

Cheyne
  • 1,964
  • 4
  • 27
  • 43