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?

Heya,

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);
}
//   -->
</script>

<div style="background-color:#ffcc00;padding:5px;width:100px;cursor:pointer;"
	onclick="JavaScript:timedRedirect()">
	Click me to redirect
</div>

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.

Perfect, many thanks for these details!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.