154

Can PHP make a redirect call after executing a function? I am creating a function on the completion of which I want it to redirect to a file located in the same root folder. Can it be done?

if (...) {
    // I am using echo here.
} else if ($_SESSION['qnum'] > 10) { 
    session_destroy();
    echo "Some error occured.";
    // Redirect to "user.php".
}
h4z3
  • 5,265
  • 1
  • 15
  • 29
amit
  • 10,133
  • 22
  • 72
  • 121
  • I serach about this and i find related this answer in http://stackoverflow.com/questions/13539752/redirect-function/13539808 – user1972007 Nov 26 '12 at 09:52

11 Answers11

302

Yes, you would use the header function.

/* Redirect browser */
header("Location: http://www.yourwebsite.com/user.php"); 
exit();

It is a good practice to call exit() right after it so that code below it does not get executed.

Also, from the documentation:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

This means you should not echo anything right before the header() function, as doing so will more than likely throw an error. Also, you will need to verify that this code gets run before any other output as well.

Amjad
  • 1,627
  • 5
  • 21
  • 41
bkildow
  • 5,143
  • 4
  • 29
  • 37
52

Using a javascript as a failsafe will ensure the user is redirected (even if the headers have already been sent). Here you go:

// $url should be an absolute url
function redirect($url){
    if (headers_sent()){
      die('<script type="text/javascript">window.location=\''.$url.'\';</script‌​>');
    }else{
      header('Location: ' . $url);
      die();
    }    
}

If you need to properly handle relative paths, I've written a function for that (but that's outside the scope of the question).

mcw
  • 3,500
  • 1
  • 31
  • 33
brianreavis
  • 11,562
  • 3
  • 43
  • 50
17

Simple way is to use:

  echo '<script>window.location.href = "the-target-page.php";</script>';
CrownFord
  • 709
  • 5
  • 15
13
   $url='the/url/you/want/to/go';
   echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';

this works for me fine.

Thusitha Sumanadasa
  • 1,669
  • 2
  • 22
  • 30
8
header( "Location: http://www.domain.com/user.php" );

But you can't first do an echo, and then redirect.

Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
code-zoop
  • 7,312
  • 8
  • 47
  • 56
3
<?php

    http_redirect("relpath", array("name" => "value"), true, HTTP_REDIRECT_PERM);

?>
user229044
  • 232,980
  • 40
  • 330
  • 338
3

As metioned by nixxx adding ob_start() before adding any php code will prevent the headers already sent error.

It worked for me

The code below also works. But it first loads the page and then redirects when I use it.

echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$redirect_url.'">';
2

You can use this code to redirect in php

<?php
/* Redirect browser */
header("Location: http://example.com/");
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
0

Yes.

In essence, as long as nothing is output, you can do whatever you want (kill a session, remove user cookies, calculate Pi to 'n' digits, etc.) prior to issuing a location header.

John Parker
  • 54,048
  • 11
  • 129
  • 129
0

if you want to include the redirect in your php file without necessarily having it at the top, you can activate output buffering at the top, then call redirect from anywhere within the page. Example;

 <?php
 ob_start(); //first line
 ... do some work here
 ... do some more
 header("Location: http://www.yourwebsite.com/user.php"); 
 exit();
 ... do some work here
 ... do some more
nixxx
  • 423
  • 1
  • 9
  • 23
-1

The header() function does this:

header("Location: user.php");