HTTP redirect error

When a user enters incorrect data, I want the page to display a message for 10 seconds and then redirected to Google. When I use this code, the page is redirected to Google without showing this message properly.

if($m==12 && $d==31) { echo "you entered wrong data"; sleep(10) ; header("Location: http://google.com"); die(); } 

How can i fix this?

+4
source share
1 answer

You cannot respond to an HTTP request with a resource and a message stating that the requested resource can be found with a different URL.

If you want to redirect after a period of time, you need to use a document-level solution (e.g. <meta> or JavaScript).

For instance:

 function redirect() { location.replace("http://google.com"); } setTimeout(redirect, 10000); 

That said ...

People read at different speeds, and people do not always pay constant attention to a particular browser window (start downloading something, and then switch to another tab is not uncommon).

I try to work with the rule of thumb that if a message is to be shown to the user, then it should be displayed until they reject it.

 <p> you entered wrong data </p> <p> <a href="http://google.com/">continue</a> </p> 
+2
source

All Articles