重定向-替代“< meta http-equiv ='refresh'/>"? [英] Redirects - alternative to "<meta http-equiv='refresh' />"?

查看:124
本文介绍了重定向-替代“< meta http-equiv ='refresh'/>"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Possible Duplicate:
Best redirect methods?

你好

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

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.

重定向由<meta http-equiv='refresh' content='=2;index.php' />处理,但是自那以后我就知道它已被贬值,并且在所有浏览器中均不起作用.

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.

是否有另一种方法可以在下面的代码中放置重定向?

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.

推荐答案

不建议使用META刷新.您的刷新标签有一个额外的=.应该是

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

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

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

You can do a refresh with a header too:

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

或使用302重定向:

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

或者用Java语言完成.

Or do it in Javascript.

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

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.

这篇关于重定向-替代“&lt; meta http-equiv ='refresh'/&gt;"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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