如何在PHP中进行重定向? [英] How do I make a redirect in PHP?

查看:108
本文介绍了如何在PHP中进行重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过使用PHP将用户重定向到其他页面?

Is it possible to redirect a user to a different page through the use of PHP?

假设用户转到www.example.com/page.php,并且我想将他们重定向到www.example.com/index.php,那么在不使用元刷新的情况下该怎么做?有可能吗?

Say the user goes to www.example.com/page.php and I want to redirect them to www.example.com/index.php, how would I do so without the use of a meta refresh? Is it possible?

这甚至可以保护我的页面免受未经授权的用户的侵害.

This could even protect my pages from unauthorized users.

推荐答案

现有答案的摘要以及我自己的2美分:

Summary of existing answers plus my own two cents:

您可以使用header()函数发送新的HTTP标头,但是必须在任何HTML或文本之前(例如,在<!DOCTYPE ...>声明之前)将其发送到浏览器.

You can use the header() function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the <!DOCTYPE ...> declaration, for example).

header('Location: '.$newURL);

2.重要信息

die() exit()

header("Location: http://example.com/myOtherPage.php");
die();

为什么要使用die()exit():每日WTF

绝对或相对URL

自2014年6月起,绝对URL和相对URL均可使用.请参阅 RFC 7231 ,它已替换了旧的

Since June 2014 both absolute and relative URLs can be used. See RFC 7231 which had replaced the old RFC 2616, where only absolute URLs were allowed.

状态码

PHP的位置"标头仍使用 HTTP 302 -重定向代码,但这是不是您应该使用的那个.您应该考虑 301 (永久重定向)或

PHP's "Location"-header still uses the HTTP 302-redirect code, but this is not the one you should use. You should consider either 301 (permanent redirect) or 303 (other).

注意: W3C提到 303-标头与许多HTTP/1.1之前的用户代理不兼容.当前使用的浏览器都是HTTP/1.1用户代理.对于其他许多用户代理(例如蜘蛛网和漫游器),情况并非如此.

Note: W3C mentions that the 303-header is incompatible with "many pre-HTTP/1.1 user agents. Currently used browsers are all HTTP/1.1 user agents. This is not true for many other user agents like spiders and robots.

HTTP标头和PHP中的header()函数

HTTP Headers and the header() function in PHP

  • What the PHP manual says
  • What Wikipedia says
  • What the W3C says

您可以使用http_redirect($url);的替代方法,该方法需要 PECL包pecl 为已安装.

You may use the alternative method of http_redirect($url); which needs the PECL package pecl to be installed.

此功能未包含303状态代码:

This function doesn't incorporate the 303 status code:

function Redirect($url, $permanent = false)
{
    header('Location: ' . $url, true, $permanent ? 301 : 302);

    exit();
}

Redirect('http://example.com/', false);

这更灵活:

function redirect($url, $statusCode = 303)
{
   header('Location: ' . $url, true, $statusCode);
   die();
}

6.解决方法

如前所述,header()仅在任何内容被写出之前才重定向工作.如果在HTML输出中调用,它们通常会失败.然后,您可以使用HTML标头变通方法(不是很专业!),例如:

6. Workaround

As mentioned header() redirects only work before anything is written out. They usually fail if invoked inmidst HTML output. Then you might use a HTML header workaround (not very professional!) like:

 <meta http-equiv="refresh" content="0;url=finalpage.html">

甚至是JavaScript重定向.

Or a JavaScript redirect even.

window.location.replace("http://example.com/");

这篇关于如何在PHP中进行重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆