How to delay the execution of the code?

advertisements

How can I delay the execution of a block of code in php?

    if ($qry->execute($leads)) {
    echo'<div class="note success">' .
            $_POST["first_name"] . ', Your entry was successful!
        </div>';

        // Set up email params

This code displays a "Successful" message then emails the users. After displaying the the message, I want to redirect the user.

Is this possible in PHP? Do I need to use JavaScript?

Thanks!


If you want to delay the execution of PHP code, you can use the sleep function. However, it appears that you want to delay a redirect for the client. You can do this with JavaScript, <meta> refresh and the Refresh header.

Using the Refresh header:

header('Refresh: «timeout in seconds»; URL=http://example.com/myurl');

Using <meta> refresh:

    <meta http-equiv="refresh" content="«timeout in seconds»; URL=http://example.com/myurl">

Using JavaScript:

setTimeout(function(){
 location.replace("/myurl");
},«timeout in milliseconds»);

The best is to use a combination of these three, and also show a message for users that have disabled these redirection technologies with a link that allows them to click through manually.