View Full Version : help in meta refresh tag
sukanya.paul
05-02-2007, 05:12 AM
hi
i need to kno if meta refresh tag can be stopped automatically after a certain amount of time..i want my page to be refreshed after every 10 sec ten times after which the page should be redirected to another page..
can some1 pls help..
it'll be appreciated a lot..
thanks ain advance..suk
djr33
05-02-2007, 05:51 AM
There is no talking between the pages. Each page is independant. To make it change on different loads (as in having a new behavior on the reload), you would need to implement some code that would keep track of it. I think the best idea would be to use PHP. Javascript might work, but that's hard to make it talk between pages. You could also use cookies with either, but I don't know if that's needed.
Here are two solutions:
<?php
$n = $_GET['n']-1;
if (!isset($_GET['n'])) {$n=10;} //10=number of refreshes
if ($n < 1) {$url = 'http://this.com/other.htm';} //page to redirect to at end
else {$url = 'http://this.com/this.php?n='.$n;}
echo '<meta http-equiv="refresh" content="10;url='.$url.'">'; //10=seconds for each refresh
?>
//OR...
<?php
$n = $_COOKIE['reload']-1;
if (!isset($_COOKIE['reload'])) {$n=10;}
setcookie('reload',$n,time()+15); //see note below
if ($n < 1) {$url = 'http://this.com/other.htm';}
else {$url = 'http://this.com/this.php';}
echo '<meta http-equiv="refresh" content="10;url='.$url.'">';
?>
The 15 second (after current time) expiration on the cookie
allows for more than 10 seconds, but it will expire to help
prevent reloading the page starting partway through the cycle.
Set this about 5 seconds longer than the refresh delay (currently 10).
Hope this helps.
The cookie solution requires cookies enabled; the first version will have a variable in the address bar. Both require PHP installed/enabled on the server (independant of the user), and also may have issues (different for each) with refreshing the pages, etc. in the middle of the cycle.
jscheuer1
05-02-2007, 05:59 AM
I don't think you can do that with meta refresh. Every ten seconds, sure. But, there is no way to pass along to the next load a count of how many times this is taking place. Javascript can do that though, put this script in the head of your page:
<script type="text/javascript">
function get(key_str) {
var query = window.location.search.substr(1);
var pairs = query.split("&");
for(var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split("=");
if(unescape(pair[0]) == key_str)
return unescape(pair[1]);
}
return '0';
}
if(get('ld')*1<101)
setTimeout("window.location.replace(window.location.href.split('?')[0]+'?ld='+[get('ld')*1+10])", 10000);
else
window.location.replace('http://www.dynamicdrive.com/');
</script>
You probably should also include for non-javascript enabled browsers a meta refresh tag with the final destination:
<meta http-equiv="refresh" content="100;url=http://www.dynamicdrive.com/">
sukanya.paul
05-02-2007, 06:11 AM
thanks..
but let me explain my problem further..
i am doin this for an online exam.
my page called random.php has to list questions from database every10 sec.these questions are generated by random numbers
$sql=mysql_query("SELECT * FROM question WHERE id='$random_digit'");
every 10 sec the is to be refreshed and new random num generated and thus new query gets executed.after 10 such queries being executed the page shud be redirected to a page upload.php
where do i place the code? i am posting my code so far..if only u cud help me..
sorry for the trouble n thanks for the help..
<?php
$n = $_GET['n']-1;
if (!isset($_GET['n'])) {$n=10;} //10=number of refreshes
if ($n < 1) {$url = 'http://this.com/uploadmain.php';} //page to redirect to at end
else {$url = 'http://this.com/this.php?n='.$n;}
echo '<meta http-equiv="refresh" content="5;url='.$url.'">'; //10=seconds for each refresh
include 'db.php';
$maximum=mysql_query("select MAX(id) as max from question");
$chk = mysql_num_rows($maximum);
$u = mysql_fetch_array($maximum);
$max=$u['max'];
//echo "maximum=" . $max;
$minimum=mysql_query("select MIN(id) as min from question");
$chk1 = mysql_num_rows($minimum);
$r = mysql_fetch_array($minimum);
$min=$r['min'];
//echo "minimum=" . $min. "<br>";
$random_digit=rand($min,$max);
echo $random_digit;
$sql=mysql_query("SELECT * FROM question WHERE id='$random_digit'");
$count=mysql_num_rows($sql);
if ($count>0)
$valid=true;
if ($valid)
{
echo "<table width='95%' border='2'>
<tr>
<th>QUESTION</th>
<th>#</th>
<th>OPTION 1</th>
<th>#</th>
<th>OPTION 2</th>
<th>#</th>
<th>OPTION 3</th>
<th>#</th>
<th>OPTION 4</th>
</tr>";
for ( $counter = 1; $counter <= 10; $counter += 1)
{
while($row = mysql_fetch_array($sql))
{
echo "<tr>";?>
<?
echo "<td>" .$row['quest'] . "</td>" ."<br>";
?>
<td align="center"> <input type="radio" name="group1" id="<?=$row[id]?>" value="<?=$row[id]?>" /> </td>
<?
echo "<td>" . $row['opt1'] . "</td>" . "<br>";?>
<td align="center"> <input type="radio" name="group1" id="<?=$row[id]?>" value="<?=$row[id]?>" /> </td>
<? echo "<td>" . $row['opt2'] . "</td>" . "<br>";?>
<td align="center"> <input type="radio" name="group1" id="<?=$row[id]?>" value="<?=$row[id]?>" /> </td>
<? echo "<td>" . $row['opt3'] . "</td>" . "<br>";?>
<td align="center"> <input type="radio" name="group1" id="<?=$row[id]?>" value="<?=$row[id]?>" /> </td>
<? echo "<td>" . $row['opt4'] . "</td>";
echo "</tr>";
}
}
echo "</table>";
}
?>
djr33
05-02-2007, 06:14 AM
This code outputs a meta tag. As with any meta tag, it must be placed in the head section, not in the body.
Use two seperate bits of PHP.
sukanya.paul
05-02-2007, 06:20 AM
ya i think i would be more comfortable with using php..but this thing is not workin..mayb the placement of my code is wrong or somethin...can ya look into the code i hv posted n lemme kno whts wrong...
plsss
thanks :)
djr33
05-02-2007, 06:21 AM
We were posting out of order (due to time writing posts), so I edited my post above. It has the info you need, I believe.
sukanya.paul
05-02-2007, 07:10 AM
its not working!!!! i dont know where i am goin wrong now.... :( :confused:
djr33
05-02-2007, 08:00 AM
Do you have a link to your page so we can see it?
Simply put, an html document looks like this:
<html>
<head>
HEADERS, etc. HERE (...the meta tag)
</head>
<body>
CONTENT HERE
</body>
</html>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.