[PHP] how can I add a delay after a line of code?

Stkr Dngr

Regular Member
Joined
Sep 14, 2017
Messages
301
Reaction score
43
I'm on a script that loads a site. I'm using header function, and I want another site to load after the one at the top. But it loads immediately after the first site, but I want users to read what's on the first site before the redirect. I want to add a delay like 7secs to allow them read before the second line loads.
Code:
<?php header('Location: http://www.site1.com/');
/*i want to add delay here*/
header('Location: http://www.site2.com/'

exit;
?>

Thanks in advance
 
Code:
<?php header('Location: http://www.site1.com/');
usleep(1000); // parameter is in microseconds
//sleep(1); // you could also use sleep.. parameter is in seconds in this case
header('Location: http://www.site2.com/');
exit;
?>

You also have error in the code, which I fixed in my example. btw, this in theory won't work because once the header is fired, it goes to the other script and the rest of the code is not executed. Your best bait would be to load the first site using file_get_content, show it and then redirect.
 
Once you send headers for URL redirection - the visitor's browser will link to the new site - your server cant control after the redirection has occurred.
If you have access to site2.com => try adding redirection there in javascript.

Other option will be to serve a html page with javascript redirects to multiple URLs after specified time intervals.
 
@kencheweb @Gogol Sorry guys, after much i forgot that only one can be called at a time, because if it loads the fists url, it would have gone out of the script and not see the second. Also like you said, by default it will load the last url. Tried it all out and concluded that the delay can only be set for one url redirect.

I would have gotten this if you didnt offer attention at first. So i thank you so much for your contribution.
 
Once you send headers for URL redirection - the visitor's browser will link to the new site

Not quite. Subsequent header( "Location: ..." ) will replace previous ones. You should call exit after header( "Location: ..." );

I want another site to load after the one at the top. But it loads immediately after the first site, but I want users to read what's on the first site before the redirect. I want to add a delay like 7secs to allow them read before the second line loads.

Consider meta refresh:
Code:
<head>
...
<meta http-equiv="refresh" content="7; url=http://site2.com/">
...
</head>

See where it says "content=7"? That's where you set the number of seconds delay before redirect.
 
look it up
php command sleep

Read what OP wants to do (above his CODE tag). PHP's sleep won't help; the browser will wait for the remainder of the document and not load the page, then when it finally does load the page (after the delay) it will immediately go to the second page. So basically the user would get a "loading" screen for seven seconds, and then immediately go to the second page.

OP wants the user to see a page and stay for 7 seconds before being redirected. Meta Refresh is the easiest way to go, JS is second. Impossible to do this in PHP.
 
Not quite. Subsequent header( "Location: ..." ) will replace previous ones. You should call exit after header( "Location: ..." );

From PHP 5.1.2 onwards - header function now prevents more than one header to be sent at once as a protection against header injection attacks.
Refer Official Doc @
http://php.net/manual/en/function.header.php

Using exit or die is recommended though. Thanks for your input.
 
sleep(time)
sleep(10)
will sleep for 10 seconds, but its very basic. You should add some text or something before redirecting
 
Back
Top