header location delay
You cannot do this with a HTTP location redirect, as this redirect will happen as soon as the browser gets the header. Instead, use a refresh redirect in the header:
header( "Refresh:5; url=http://www.example.com/page2.php", true, 303);
This should work on modern browsers, however it is not standardized, so to get the equivalent functionality would be do use a meta refresh redirect (meaning you'd have to output a full HTML too):
<meta http-equiv="refresh" content="5;url=http://www.example.com/page2.php">
From the Wikipedia page:
Used in redirection, or when a new resource has been created. This
refresh redirects after X seconds. This is a proprietary, non-standard
header extension introduced by Netscape and supported by most web
browsers.
PHP - Delay on redirect
You can send php header with timeout refresh. http://php.net/manual/en/function.header.php
<?php
header( "refresh:5; url=wherever.php" );
?>
PHP redirect after 5 seconds
Use header Refresh.
It is simple:
header("Refresh:5; url=register.php");
It should work, make sure no output is before this header.
can you use a delay of less than a second on header(refresh:...) with PHP?
Tried it and it works
For example: header("refresh: 0.25;");
header("refresh: 0.1;");
header("refresh: 0.75;");
header("refresh: 0.5;");
all seem to work.
Delay unlink after Header
What you are trying to do is in practice impossible to do with PHP, because PHP is single threaded and executed from start-to-finish for each request. This means that the header
redirect is not done until all the code in your script is completed. This means that the script will first wait, then delete the file, then attempt to redirect to a non-exiting file.
Instead I purpose another approach; using and storing a unique hash that is usable only once.
Check if the user is logged in. If he or she is logged in, generate a unique hash and insert it in the database e.g. INSERT INTO hashes (hash, used) VALUES ($myUniqueHash, 0)
. Then redirect the user to a PHP-file with the hash in the request query, e.g.
header("Location: serve_file.php?hash=$myUniqueHash");
This file will check the value of $_GET['hash']
against the table that contains the hashes, and check if the value of used
is 0
. If this is the case it will update the used
column to 1
and serve the file as a proxy. If the hash is not found, or if the value of the used
column is not 0
it will return an error.
This way we can serve a (secret) file only once for logged in users.
PHP delay redirect back to a page
You could use a meta tag:
<?php if (!empty($_SERVER['HTTP_REFERER'])) { ?>
<meta http-equiv="refresh" content="2;url=<?php echo $_SERVER['HTTP_REFERER'] ?>">
<?php } ?>
Otherwise, you can use a JavaScript solution:
<head>
<script type="text/javascript">
setTimeout("window.location = '<?php echo $_SERVER['HTTP_REFERER'] ?>'", 2000);
</script>
</head>
How To Add A Delay To PhP Header?
Sleep
function will block code to be executed after.
Maybe what you are looking for is html meta tag refresh
Like this:
<META http-equiv="refresh" content="5;URL=http://www.example.com/mypage">
where 5 is measure in seconds.
Related Topics
Nl2Br() Equivalent in JavaScript
PHP - Override Existing Function
Share Session Between Two Websites
Adjusting Time Zone in PHP with Datetime/Datetimezone
Jquery $.Post Processing JSON Response
Why Don't PHP and JavaScript's Timestamps Match
Patterns for PHP Multi Processes
Get the Auto-Generated Id After an Insert
No Mail Received in Inbox with Xampp 1.8.0, Mercurymail and Mail()
Working with Japanese Filenames in PHP 5.3 and Windows Vista
How to Allow a User to Download a File Which Is Stored Outside of the Webroot
Display Zend_Form_Element_Radio on One Line
Parsing Extremely Large Xml Files in PHP
Un-Encrypting/Re-Encrypting a Coldfusion Encrypted String in PHP