我有一个url shortner脚本。有一个'forward.php'文件,用于在缩短查询时重定向。但在此文件中,我想在将其重定向到原始网址之前显示一些文字或广告5-10秒。
我也尝试sleep(),flush()以及所有的帮助。
谢谢。
代码:
<?php
ob_start();
require("lib/config.php");
require("lib/common.php");
if( isset($_SERVER['QUERY_STRING']) ){
$i = $_SERVER['QUERY_STRING'];
}else{
$i = $_SERVER['REQUEST_URI'];
$i = str_replace("/","",$i);
}
$suffix = $i{0};
$result = mysql_query("SELECT id,url FROM urls WHERE short_url = '$i'",DBH) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
$id = $row['id'];
$url = $row['url'];
mysql_query("UPDATE urls SET hits=hits+1 WHERE id = '{$id}'",DBH);
if ($_SESSION['config']['bar'] == true)
{
$meela_urllow = stripslashes(str_replace(",", "%2C", $url));
$meela_urllow = strtolower($meela_urllow);
if ((strpos($meela_urllow, "youtube")))
{
header('HTTP/1.1 301 Moved Permanently');
header("Location: ".stripslashes(str_replace(",", "%2C", $url)));
}
if ((strpos($meela_urllow, "facebook")))
{
header('HTTP/1.1 301 Moved Permanently');
header("Location: ".stripslashes(str_replace(",", "%2C", $url)));
}
if ((strpos($meela_urllow, "google")))
{
header('HTTP/1.1 301 Moved Permanently');
header("Location: ".stripslashes(str_replace(",", "%2C", $url)));
}
} else {
header('HTTP/1.1 301 Moved Permanently');
header("Location: ".stripslashes(str_replace(",", "%2C", $url)));
}
exit;
}
header('HTTP/1.1 301 Moved Permanently');
header("Location: http://".$_SESSION['config']['domain']);
exit;
?>
答案 0 :(得分:0)
如果要在重定向之前显示文本,则需要使用带有刷新值的header()
函数:
header('Refresh: 5; url=some_url.php'); // tell browser to wait 5 sec then redirect
睡眠php脚本与暂停它一样...脚本只会挂起并且什么都不做,使用输出缓冲区无法帮助,因为必须在发送任何HTTP正文之前发送Location
标头,所以只有选项才能使用标题刷新。
这与使用header()
函数相同,但不需要php代码:https://stackoverflow.com/a/737003/555097
答案 1 :(得分:0)
或者您可以使用javascript为您完成任务! 只是一个js脚本,一旦页面加载,然后重定向
,等待5秒<script>
setTimeout("redir()",5000);
function redir()
{
window.location.href = 'your url here';
}
</script>