php 重定向 - 替代“<meta http-equiv='refresh' />”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3396371/
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 09:35:49  来源:igfitidea点击:

Redirects - alternative to "<meta http-equiv='refresh' />"?

phpmysqlredirect

提问by Dave

Possible Duplicate:
Best redirect methods?

可能的重复:
最佳重定向方法?

Hello

你好

I am working with some legacy code that includes a module for user registration / login. There is a block that queries the DB to see if the user is logged in, then re-directs to the login page.

我正在使用一些遗留代码,其中包括一个用于用户注册/登录的模块。有一个块可以查询数据库以查看用户是否已登录,然后重定向到登录页面。

The re-direct is handled by <meta http-equiv='refresh' content='=2;index.php' />but I have since learnt this is depreciated, and doesn't work in all browsers.

重定向由处理,<meta http-equiv='refresh' content='=2;index.php' />但我后来了解到这是折旧的,并且不适用于所有浏览器。

Is there an alternative way to put a re-direct within the code below?

有没有其他方法可以在下面的代码中放置重定向?

    $username = mysql_real_escape_string($_POST['username']);
    $password = md5(mysql_real_escape_string($_POST['password']));

    $checklogin = mysql_query("SELECT * FROM users WHERE username = '".$username."' AND password = '".$password."'");

    if(mysql_num_rows($checklogin) == 1)
    {
        $row = mysql_fetch_array($checklogin);
        $email = $row['email'];

        $_SESSION['username'] = $username;
        $_SESSION['email'] = $email;
        $_SESSION['LoggedIn'] = 1;

        echo "<h1>Success</h1>";
        echo "<p>We are now redirecting you</p>";
        echo "<meta http-equiv='refresh' content='=2;index.php' />";
    }
    else
    {
        echo "<h2>Error</h2>";
        echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>";
    }

Many thanks for any pointers.

非常感谢您的指点。

回答by jmz

META refresh is not deprecated. Your refresh tag has an extra =. It should be

META 刷新未弃用。您的刷新标签有一个额外的 =。它应该是

<meta http-equiv='refresh' content='2;index.php' />

You can do a refresh with a header too:

您也可以使用标题进行刷新:

header("Refresh: 2;index.php");

Or use 302 redirection:

或者使用 302 重定向:

header("Location: /index.php");

Or do it in Javascript.

或者用Javascript来做。

And the best method? Use a META refresh tag in the <head>section. Rationale for this is that IE does not save headers when it uses cached version of a page.

最好的方法是什么?在该<head>部分中使用 META 刷新标记。这样做的理由是 IE 在使用页面的缓存版本时不保存标头。

回答by Pekka

You can use header()before any content is sent to the server:

您可以header()在任何内容发送到服务器之前使用:

header("Location: index.php");
die();

this will send the browser a 302response and cause it to open the other page instead.

这将向浏览器发送302响应并使其打开另一个页面。

Always make sure you DO NOT output any content after issuing a `header("Location"). Anything you output will still be sent to the browser!

始终确保在发出 `header("Location") 后不要输出任何内容。您输出的任何内容仍将发送到浏览器!

The easiest way to achieve that is usually issuing a die()after the header("Location:") call.

实现这一目标的最简单方法通常是die()在 header("Location:") 调用之后发出一个。

回答by defines

In addition to the headerfunction mentioned by Pekka, you could alternatively use javascript - this is the behavior intended to replace the meta refresh which has been deprecated.

除了headerPekka 提到的功能之外,您还可以使用 javascript - 这是旨在替换已弃用的元刷新的行为。

<script>top.location = 'index.php';</script>

Simply replace your meta line with the above.

只需将您的元行替换为上述内容即可。

You can even add a timeout as follows:

您甚至可以按如下方式添加超时:

<script>setTimeout('top.location = \'index.php\'', 2000);</script>

The above will wait 2 seconds before redirecting.

以上将在重定向前等待 2 秒。

回答by Stefan Hoth

First of all: Meta is only allowed in the head-section of a HTML-document so putting it out after h1 and p in the body is wrong and won't work at all in most browsers.

首先:Meta 只允许出现在 HTML 文档的 head-section 中,因此将它放在 body 中的 h1 和 p 之后是错误的,并且在大多数浏览器中根本不起作用。

For showing a message for 2 seconds and redirect afterwards you'd have to use javascript, php can't do that for you.

要显示 2 秒钟的消息并在之后重定向,您必须使用 javascript,php 无法为您执行此操作。

Plain javascript would be using setTimeout (value in milliseconds)

普通的 javascript 将使用 setTimeout(以毫秒为单位的值)

setTimeout("location.href = 'index.php';",2000);

If you want to use jQueryyou could use the delay()-function.

如果你想使用jQuery,你可以使用delay()函数。

回答by Piotr Pankowski

header ("Location: index.php?success=1");
exit;

and in index.php

并在 index.php

if (!empty ($_GET['success'])) {
    echo "<h1>Success</h1>";
}