17

It seems that it is not advisable to use

<meta http-equiv=REFRESH CONTENT=3;url=url>

for redirects but instead use

header('Location: url')

However, I would like to show the user some message and allow them some time to read it before redirecting. Is there a way to do it without meta?

MPelletier
  • 16,256
  • 15
  • 86
  • 137
zhenming
  • 1,059
  • 3
  • 11
  • 14

8 Answers8

40

Try use "refresh" header:

header('Refresh: 3;url=page.php');

Also, you can look at this Question Refresh HTTP Header.

Community
  • 1
  • 1
Dador
  • 5,277
  • 1
  • 19
  • 23
  • isn't this as same as ``? – Taha Paksu Jun 22 '12 at 10:38
  • It does the same, but via different ways. – Dador Jun 22 '12 at 10:41
  • is placed inside html's tag and I wonder if it's acting like header() in the http stream. If that's true, they both are the same. – Taha Paksu Jun 22 '12 at 10:45
  • thanks, it works. just wondering, which is the preferred method? meta refresh or header refresh? – zhenming Jun 22 '12 at 16:35
  • The header is preferred, IMO. Just because it is where it is supposed to be. Instead of just being: Here are the headers and this is the HTML. With the meta it is: Here are the headers, here's the HTML... Oh! Wait I forgot this header! Here's the rest of the HTML, now deal with it. – brunoais Apr 15 '13 at 11:05
  • how can i do this in codeigniter where we use functions instead of file.php so it may be like this header('Refresh:10; url=function); – Aitazaz Khan Oct 30 '14 at 11:04
  • I have used it into my code but it not working. can anyone help me ? – huykon225 Mar 10 '17 at 16:27
  • @huykon225 maybe you have some html/prints before using header() function? (header() function should be called before sending any data to browser) – Dador Mar 10 '17 at 19:56
  • yes @Dador . I create this code on a Conditional expression – huykon225 Mar 11 '17 at 15:36
6

There is nothing wrong with using the meta refresh tag.

<meta http-equiv="refresh" content="5;URL='http://example.com/'" />

That tag says wait 5 seconds and redirect to example.com. This tag isn't a problem unless users are on IE6 and it still works, just breaks the history buttons.

Using JavaScript is an option, but make sure you include a link saying "If you are not automatically redirected, please click here". You should actually include that link either way.

Brandon
  • 16,382
  • 12
  • 55
  • 88
  • the meta refresh tag seems to be deprecated according to this http://www.w3.org/TR/WCAG10-HTML-TECHS/#meta-element – zhenming Jun 22 '12 at 16:17
5

php way to set header, will redirect you to test.php in 5 seconds:

header( "refresh:5;url=test.php" );

call before any actual output is sent.

And in javascript:

setTimeout(function () {
   window.location.href= url; // the redirect goes here
},5000); // 5 seconds
Sanjay
  • 1,570
  • 1
  • 13
  • 30
4

Header tags are sent on page load, to the browser, so that it can quickly redirect the user to the desired page without bothering to render it or even load it into the history. As such, you can't call a redirect once the page has already loaded, as the headers have already been dealt with.

You can instead perform this with:

header( "refresh:5;url=wherever.php" );

Which basically sets the <meta> tag in the headers of the page itself, meaning you don't need to write the tag out.

Death
  • 1,999
  • 12
  • 14
2

By what you guys are saying, theoretically this should work then:

URL: http://www.example.com/ticketgen/index.php?success=1&redir=1

    <?php
 $myredir = ($_GET['redir']);
    if ($myredir == 1)
    {
        header( "refresh:5;url=http://www.example.com/ticketgen/" );
    }
?>

But it does nothing. I also have it at the VERY TOP of the page so it can send the headers.

it doesn't work in Firefox i just found out.

Gordon
  • 312,688
  • 75
  • 539
  • 559
Patrick K.
  • 21
  • 2
1

You can do it with a small piece of javascript:

<script type="text/javascript" language="JavaScript">location.href = 'otherpage.php';</script>

Of course, this will depend on the person having JavaScript enabled.

Obviously, to set the delay you can use something like setTimeout:

<script type="text/javascript" language="JavaScript">
    setTimeout(function () {
                      location.href = 'stackoverflowhelp.php'; 
               }, 5000);
</script>
Jason Larke
  • 5,289
  • 25
  • 28
1

I think really the best way is header("Refresh: 10;url=../index.php"); Like what i've done with my work.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
-1

https://codingislove.com/redirect-pages-php/

check out the above article, where they clearly explained about how to redirect the pages in PHP by setting time.

Redirecting code without time set: header('location:URL ADDRESS');

Redirecting code with three seconds time set: header('refresh:3; url=URL ADDRESS');

Rajeshdn
  • 1
  • 2