php PHP页面重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2112373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:04:09  来源:igfitidea点击:

PHP page redirect

phpredirectheader

提问by amit

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?

PHP 可以在执行函数后进行重定向调用吗?我正在创建一个函数,完成后我希望它重定向到位于同一根文件夹中的文件。可以做到吗?

if (...) {
    // I am using echo here.
} else if ($_SESSION['qnum'] > 10) { 
    session_destroy();
    echo "Some error occured.";
    // Redirect to "user.php".
}

回答by bkildow

Yes, you would use the headerfunction.

是的,您将使用标题功能。

/* 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.

在它exit()之后立即调用是一个很好的做法,这样它下面的代码就不会被执行。

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.

请记住,必须在发送任何实际输出之前调用 header(),无论是通过普通的 HTML 标记、文件中的空行还是来自 PHP。使用 include() 或 require() 函数或其他文件访问函数读取代码并且在调用 header() 之前输出空格或空行是一个非常常见的错误。使用单个 PHP/HTML 文件时存在同样的问题。

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.

这意味着您不应该在header()函数之前回显任何内容,因为这样做很可能会引发错误。此外,您还需要验证此代码是否在任何其他输出之前运行。

回答by brianreavis

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

使用 javascript 作为故障保护将确保用户被重定向(即使标题已经发送)。干得好:

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

如果您需要正确处理相对路径,我已经为此编写了一个函数(但这超出了问题的范围)。

回答by CrownFord

Simple way is to use:

简单的方法是使用:

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

回答by Thusitha Sumanadasa

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

this works for me fine.

这对我有用。

回答by code-zoop

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

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

但是你不能先做一个回声,然后重定向。

回答by user3164291

<?php

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

?>

回答by user2634873

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

正如所提及般通过nixxx添加ob_start()添加任何PHP代码将阻止已经发出错误的头前。

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

回答by Mahdiyah Al-kaff

You can use this code to redirect in php

您可以使用此代码在php 中重定向

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

回答by nixxx

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 文件中包含重定向而不必将其放在顶部,您可以在顶部激活输出缓冲,然后从页面内的任何位置调用重定向。例子;

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

回答by llothcat

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

实际上,我在基于 php 的 cms 的代码中发现了这一点。

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

重定向('?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.

我也想在活动结束后刷新页面,并将其放置在更好的位置效果很好。