PHP page redirect

PhpRedirectHeader

Php Problem Overview


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".
}

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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).

Solution 3 - Php

Simple way is to use:

  echo '<script>window.location.href = "the-target-page.php";</script>';

Solution 4 - Php

   $url='the/url/you/want/to/go';
   echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';

this works for me fine.

Solution 5 - Php

header( "Location: http://www.domain.com/user.php" );

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

Solution 6 - Php

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.'">';

Solution 7 - Php

<?php

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

?>

Solution 8 - Php

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;
?>

Solution 9 - Php

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.

Solution 10 - Php

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

Solution 11 - Php

The header() function does this:

header("Location: user.php");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionamitView Question on Stackoverflow
Solution 1 - PhpbkildowView Answer on Stackoverflow
Solution 2 - PhpbrianreavisView Answer on Stackoverflow
Solution 3 - PhpCrownFordView Answer on Stackoverflow
Solution 4 - PhpThusitha SumanadasaView Answer on Stackoverflow
Solution 5 - Phpcode-zoopView Answer on Stackoverflow
Solution 6 - Phpuser2634873View Answer on Stackoverflow
Solution 7 - Phpuser3164291View Answer on Stackoverflow
Solution 8 - PhpMahdiyah Al-kaffView Answer on Stackoverflow
Solution 9 - PhpJohn ParkerView Answer on Stackoverflow
Solution 10 - PhpnixxxView Answer on Stackoverflow
Solution 11 - Phpuser235064View Answer on Stackoverflow