真实结果后重定向到页面

时间:2011-04-18 17:06:44

标签: php

您好,感谢您的时间。 我有一个名为get getresult.php的文件,它在mysql服务器上为登录的用户检查用户名和密码。

如果登录成功,我只是将其重定向到页面index.php。 感谢您提供任何帮助。

<?php
// version 1.1
if (isset($_POST['password'])) // if the password is set then the  form has been submitted on login.php page
{
 include("config.php");
 $username =  mysql_real_escape_string($_POST['username']);
 $password = mysql_real_escape_string($_POST['password']);
 $qstr = "SELECT * from members where username ='$username' and password ='$password'";

 $result = mysql_query($qstr);
 if (mysql_num_rows($result)) echo "<font color=#008000><Center><b>**Successful Login**</b></Center></font>" 
 **//// I thought maybe this would be the section for the redirection code****
;

else echo "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>";
 mysql_close();
}
else echo "<font color=#ff8000><Center><b>**No login attempted**</b></Center></font>";

?>

4 个答案:

答案 0 :(得分:3)

登录检查后

执行此操作:

header("Location: newPage.php"); // replace newpage with whatever you want

<强>更新

标题 BEFORE 任何回显:

header("Location: newPage.php"); // replace newpage with whatever you want
if (mysql_num_rows($result)) echo "<font color=#008000><Center><b>**Successful Login**</b></Center></font>" 

答案 1 :(得分:2)

你必须确保之前没有回音或打印任何内容,然后你可以这样做:

header("Location: http://www.yoursite.com");

或者,如果你想在打印东西后重定向,我通常会有一个打印javascript的函数

function refresh_to( $location = 'index.php'){
    $output = '<script> window.location.href = '.$location.'; </script>';
    return $output;
}

之后再打电话。

答案 2 :(得分:0)

标题(“位置:/ my / new / location”);

答案 3 :(得分:0)

header("refresh:5;url=http://google.com");
// version 1.1
if ( isset($_POST['password']) ) { // if the password is set then the form has been submitted on login.php page
    include("config.php");
    $username =  mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $qstr = "SELECT * from members where username ='$username' and password ='$password'";

    $result = mysql_query($qstr);
    if (mysql_num_rows($result)) {
        $return = "<font color=#008000><Center><b>**Successful Login**</b></Center></font>";
    } else {
        $return = "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>";
    }
    mysql_close();
} else {
    $return "<font color=#ff8000><Center><b>**No login attempted**</b></Center></font>";
}

// Do the output    
print($return);

请记住单独的逻辑和输出,这将是您编码的好习惯。

注意我使用header,然后执行print,同时在header我放refresh 5(秒),这将为您的访问者提供阅读时间注意你给(重点是,如果你发出通知,但他们没有时间阅读它。)