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

查看:38
本文介绍了如何在 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?

这甚至可以保护我的页面免受未经授权的用户的攻击.<​​/p>

This could even protect my pages from unauthorized users.

推荐答案

现有答案的总结加上我自己的两分钱:

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: https://example.com/myOtherPage.php");
die();

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

Why you should use die() or exit(): The Daily WTF

绝对或相对网址

自 2014 年 6 月起,可以使用绝对网址和相对网址.请参阅 RFC 7231 已替换旧的 RFC 2616,其中只允许使用绝对 URL.

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 的Location"-header 仍然使用 HTTP 302-重定向代码,这个是临时的"重定向,可能不是您应该使用的.您应该考虑 301(永久重定向)或 303(其他).

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

注意:W3C 提到 303-header 与许多 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

您可以使用 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('https://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("https://example.com/");

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

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