PDA

View Full Version : pause Xsec before redirect in a php IF ELSE (see inside)



noobster
03-30-2007, 02:10 PM
Hi, hope the title was clear so that you arrived here...

i will first show my code then explain

<?php
#Valid news?
if ($news_bol > 0 && ($debut <= $today && $fin >= $today))
{
echo "<script type='text/javascript'>location.href='news.php';</script>";
}
# Valid video?
else if ($videos_bol > 0 && ($debut_v <= $today && $fin_v >= $today))
{
echo "<script type='text/javascript'>location.href='video.php';</script>";
}
else
{
echo "<script
type='text/javascript'>location.href='promo.php';</script>";
}
?>

here it is, so i check a value in php, if the value is right, i execute a javascript redirect, else i exec another, i first tried to pause my page with a php sleep() but that brings problems with my other pages executed at the same moment.so maybe there is a way in the javascript part of this?

can someone tell me how to pause the execution of the redirect for X seconds?
it would be so helpfull .

Thanks already.

Twey
03-30-2007, 02:15 PM
You shouldn't be using this here. Use PHP to output a Refresh header:
header('Refresh: 5;url=video.php');

noobster
03-30-2007, 02:19 PM
EDIT:
Should this work? cause it dosn't seem to do:
<?php
#premier IF pour voir si il existe une NEWS valide
if ($news_bol > 0 && ($debut <= $today && $fin >= $today))
{
header('Refresh: 5;url=news.php');
}
# ELSE IF pour voir si il esxite une VIDEO valide
else if ($videos_bol > 0 && ($debut_v <= $today && $fin_v >= $today))
{
header('Refresh: 5;url=video.php');
}
else
{
header('Refresh: 5;url=promo.php');
}
?>

yes but the redirect is conditionnal, can i do multiple conditions redirect with header function?

cause header needs to be first , without spaces and so on...i 'll have a look that way but i don't know if this will be what i need.

thanks for the help anyway.

Twey
03-30-2007, 02:49 PM
yes but the redirect is conditionnal, can i do multiple conditions redirect with header function?Yes, there is no problem, so long as you don't output anything before redirecting. If your code style makes this difficult (which is a bad design, by the way), the easy way out is to use output buffering to buffer everything then output it at the end.

noobster
03-30-2007, 02:54 PM
Allright my redirection works, way better, thanks for that

i try to do what i can at coding, you can't be good in one day :rolleyes:


could you tell me what is so wrong in the code i just send earlier? so that i could improve?

Twey
03-30-2007, 03:00 PM
Output buffering works like so:
ob_start();
// Do some things that produce output
$v = ob_get_clean();$v then contains the output produced. This can be wrapped around the whole page, and the result of ob_get_clean() printed directly at the end:
print ob_get_clean();... thus easily solving the no-output-before-headers problem, although at the cost of some additional memory usage for the server.

noobster
03-30-2007, 03:05 PM
Thanks i'll keep that in mind although header redirection works, could do that before anything else .

Thanks