I'd like to redirect the user to another page, i.e. after a login check.
I'm used to doing this via header location directive, it's all ok, but, if I'd like to print a message and redirect the user in 2/3 seconds, like on this forum when you login?

Is it feasible via PHP?

7 Years
Discussion Span
Last Post by BigFormat


At a guess you could redirect them from say .... login.php to logging.php which echos the message you want!

Then on this page insert a simple piece of redirect javascript such as:

<script type="text/JavaScript">
redirectTime = "1500";
redirectURL = "http://www.yourwebsite.com";
function timedRedirect() {
	setTimeout("location.href = redirectURL;",redirectTime);
//   -->

<div style="background-color:#ffcc00;padding:5px;width:100px;cursor:pointer;"
	Click me to redirect

This will then redirect them after the amount of time you specify to the url you specify! :)


Or you could just do:

header("Refresh: 3; url=http://www.google.com");
echo "You will be redirected to Google in 3 seconds...";

Note, the echo must be after the header, or this will fail.


Which is the difference between the two solutions? I'm using the syntax from Atli's post right now and it fullfills its aim, but I'd like to know the differences, just to learn something and use it for future reference...


The one justted posted uses client-side code, JavaScript, to re-set the location being displayed, effectively redirecting the page to another URL.
Being client-side it can be somewhat unreliable, seeing as clients are free to turn of JavaScript and ignore the code completely.

The one I posted uses server-side code to send the HTTP Refresh header, which tells the browser it should refresh the page after the specified amount of seconds using the URL provided.
This method will work on anything worth calling a browser, and I doubt many browsers even provide an option to turn it off.

They will both work with most clients, but I prefer the server-side version. It's less hassle and more reliable.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.