一段时间后执行php程序

i have a url shortner script. There is a 'forward.php' file which is used to redirect the queries if it is shortened. But in this file i want to show some text or ads for 5-10 seconds before redirecting it to the original url.

I also try sleep(), flush() and all and nothing help.

Thank you.

Code:

    <?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;
?>

you need to use header() function with refresh value if you want do display text before redirect:

header('Refresh: 5; url=some_url.php'); // tell browser to wait 5 sec then redirect

sleeping a php script is same as pausing it... script will just hang and do nothing, using output buffers cannot help since Location header must be sent before any HTTP body is sent, so only option is using a header Refresh.

This is the same as using header() function but php code is not needed: https://stackoverflow.com/a/737003/555097

Or you can use javascript to do thi task for you! just a js script that waits for 5 seconds once page loads and then redirect

<script>
setTimeout("redir()",5000);
function redir()
{
   window.location.href = 'your url here';
}
</script>