PHP page redirect [duplicate]

2019-01-02 21:00发布

问题:

This question already has an answer here:

  • How to make a redirect in PHP? 28 answers

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
        }

回答1:

Yes, you would use the header function.

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

It is a good practice to call exit() right afterwords 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.



回答2:

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



回答3:

Simple way is to use:

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


回答4:

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

this works for me fine.



回答5:

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

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



回答6:

<?php

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

?>


回答7:

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.



回答8:

I would probably set the message as a session variable, redirect the user to another page which displays the message and destroy the session.



回答9:

The header() function in PHP does this, but make sure that you call it before any other file contents are sent to the browser or else you will receive an error.

JavaScript is an alternative if you have already sent the file contents.



回答10:

actually, I found this in the code of a php based cms.

redirect('?module=blog', 0);

so it is possible. In this case, you are logged in at the admin level, so no harm no foul (I suppose). the first part is the url, and the second? I can't find any documentation for what the integer is for, but I guess it's either time, or data since it is attached to a form.

I, too, wanted to refresh a page after an event, and placing this in a better spot worked out well.



回答11:

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


回答12:

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


回答13:

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


回答14:

The header() function does this:

header("Location: user.php");


回答15:

I serach about this and i find related this answer in

https://stackoverflow.com/questions/13539752/redirect-function/13539808

function redirect_url($path)
{
  header("location:".$path);
  exit;
}


标签: