PDA

View Full Version : Iframe Question



playzfer
09-20-2008, 01:55 AM
Hi

I have trying to find a way to count the amount of page views within the iframe, is it possible to do? I know it is possible outside of iframe in php... basically i want it to goto another URL after 5 page views. Any ideas?

schorhr
09-20-2008, 01:57 PM
Since you can use a regular url for a iframe and considered using php, why bother with javascript?

As I understand you want to count ALL iframe-page-visits.

<iframe src="page123.php" ...

page123.php with a very simple counter:


<?php //there can't be anything before this.
//No space, no linebreak, no html, due to the header redirect!
if(!file_exists("count.txt"))
{$counter=fopen("count.txt", "a");}
else
{$counter=fopen("count.txt", "r+");}
$filedo=fgets($counter,100);
$filedo=$filedo+1;
rewind($counter);
fputs($counter,$filedo);
fclose($counter);
if($filedo>4)
{header('Location: SOMEOTHERPAGEURL.htm');}
else if($filedo>4)
{header('Location: SOMEOTHERPAGEURL2.htm');}

?>

tacticious
09-22-2008, 04:51 PM
I'd go with sessions over an actual file.


Source code of the PHP file called by the iFrame:


session_start();

if(!$_SESSION['counter']) $_SESSION['counter'] = 0;
$_SESSION['counter']++;

if($_SESSION['counter'] == 5) header("Location: counterabove5.html");
else header("Location: counterbelow5.html");


Or, possibly even cookies.. Cookies will rely on user settings though. Sessions rely strictly on server settings, so given all your server settings are okay to use sessions, that very small script will be perfect for what you're using. Also, the session will be wiped upon a duration of inactivity and/or a browser restart.

schorhr
09-22-2008, 05:17 PM
Of course, then a file (or mysql entry) would not be needed.
I thought playzfer wanted to redirect different users that visited the page, not the same person that visits a page again.

tacticious
09-22-2008, 09:50 PM
Of course, then a file (or mysql entry) would not be needed.
I thought playzfer wanted to redirect different users that visited the page, not the same person that visits a page again.

Hmm... I took it the other way but it's really not clear. I guess either way, he's got a solution now :)