views:

73

answers:

4

Please Pardon if the question sounds silly, but nevertheless its a question which I want to know. :)

How can I redirects which display that you are being redirected (like older Gmail and LinkedIn). Whenever I tried to do that, I got errors saying that Headers were already sent.

Then somebody here told me that I should not output any markup before redirection (like facebook login). So how do I go about it and display something nice during redirection??

+1  A: 

Those redirections are not done via "normal" redirection HTTP headers. Instead they display a page and use either a META Refresh or some Javascript to navigate to the new page.

Personally I find both methods not very nice, both for users and for search engines. Using HTTP headers that also signify why there is a redirect (Permanently moved, temporary, &c.) are way better imho.

Joey
ya javascript is there, ever more like the new Gmail which imitates a sort of redirection. but please explain these two methods a little in detail :)
OrangeRind
+3  A: 

you want to use meta redirects. they enable you to show a page, and after a few seconds this page will send you to the new page. all you have to do is add a tag to the portion of your 'something nice' redirection page.

here's a quick tutorial on meta redirects: http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm

you want to do something like this:

<html>
  <head>
    <meta http-equiv="refresh" content="2;url=whereto">

were 2 is the number of seconds to display your page and whereto is where you want to send your user

Igor
All of the http-equiv meta tags have an equivalent HTTP header (as the name suggests). So a nicer solution, seeing as you're able to from PHP, is to set a 'Refresh: 2;url=http://example.com' header, and not pollute your markup
Gareth
+2  A: 

You need to output your page, which will include the following META keyword:

<html>
  <head>
    <!-- redirection to example.com in 5 seconds -->
    <meta http-equiv="refresh" content="5;url=http://example.com/" />
    ...
  </head>
  <body>
  ...

Read the following article for more help: http://en.wikipedia.org/wiki/Meta_refresh

Anax
A: 

If you are getting the headers already sent then that means you were trying to redirect with a PHP header() redirect and you had output on the screen before calling the header() function, 1 solution to that is to use PHP's Output Buffering which will then allow you to call the header() redirect anywhere on the page and not get that error.

To show a message you could use the meta method mentioned in some other answers maybe even throw in a javascript redirect with it, just to be safe however the way I would do it would be something like this below

<?PHP
ob_start();

//show a message
echo 'You will be redirected in 10 seconds';  

//amount of time to show the message above before redirecting
sleep(200);

//redirect
header('http://domain.com');
?>

I did not test this but you should get the idea, actually now that I think about it, sleep() might not work correctly as expected with output buffering

jasondavis
This can't possibly work. First, the redirect needs to be a `header('Location: http://domain.com');` but apart from that, it's an HTTP header. That means the instruction to redirect to another page comes *before* the body of the response and therefore nothing will be visible. Regardless of whether you do a `sleep` on the server side. It's simply not how HTTP works :-)
Joey
@Johannes Rössel thanks for the clarification
jasondavis