PHP 标头和刷新不起作用

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

PHP header and refresh not working

phpheaderrefresh

提问by Sasindu H

My server running in PHP 5.2.9, When I using refresh and headerfunctions it's not working. Here is my code

我的服务器运行在PHP 5.2.9 中,当我使用刷新和标头函数时,它不起作用。这是我的代码

header("location: index.php");

header( "Refresh: 0;" );

Previously I'm working in a different server, It's working correctly. How can I solve this problem?

以前我在不同的服务器上工作,它工作正常。我怎么解决这个问题?

this is my complete code

这是我的完整代码

if($_POST['sign_in'])
{
    $email = $_POST['email'];
    $password = $_POST['password'];
    $sql = "SELECT * FROM tbl_members WHERE m_email='$email' and m_password='$password'";
    $res = mysql_query($sql);
    $count = mysql_num_rows($res);
    if($count==1)
    {
        session_register("email");
        header("location: index.php");
    }
    else
    {
        $sql = "SELECT * FROM tbl_temp_members WHERE email='$email'";
        $res = mysql_query($sql);
        $count = mysql_num_rows($res);
        if($count==1)
        {
            echo "Login unsuccessful,Account still not activated";  
        }
        else
        {
            echo "Login unsccessful";
        }
    }
}

回答by cem

Location and Refresh both require an absolute URI(and it's "Location" instead of "location").

Location 和 Refresh 都需要绝对 URI(它是“Location”而不是“location”)。

Try this one:

试试这个:

header('Location: http://absolute.uri/file.ext');

If that does not help, check my answer for any "strange" PHP problem;)

如果这没有帮助,请检查我的答案是否有任何“奇怪的”PHP 问题;)

回答by Johann du Toit

Depends on what you are trying to do. Are you trying to cause a redirect ? If this is the case you could simply use header('Location: http://www.example.com/');but if you want to refresh after a certain amount of time you can use:

取决于你想做什么。你想引起重定向吗?如果是这种情况,您可以简单地使用,但如果您想在一定时间后刷新,则可以使用:header('Location: http://www.example.com/');

header( "refresh:5;url=wherever.php" ); 
echo 'You\'ll be redirected in about 5 secs. ';
echo 'If not, click <a href="wherever.php">here</a>.';

Got the example code from - http://php.net/manual/en/function.header.php- maybe worth a read too.

从 - http://php.net/manual/en/function.header.php获取示例代码- 也许也值得一读。

回答by RRStoyanov

(1) you don't need the refresh header if you have the location one

(1) 如果您有位置一,则不需要刷新标头

(2) add exit;in the end

(2)exit;在最后添加

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

第二个特殊情况是“Location:”标题。它不仅会将此标头发送回浏览器,而且还会向浏览器返回 REDIRECT (302) 状态代码,除非已经设置了 201 或 3xx 状态代码。

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

回答by M. S.M

Then better use JavaScript for that. Append this code before :

那么最好为此使用 JavaScript。在此之前附加此代码:

<script> setTimeout(function() { window.location = "index.php"; }, 2000); </script>