繁体   English   中英

header()未重定向到PHP中的页面

[英]header() is not redirecting to the page in PHP

我有以下PHP printExam.php页面:

    <?php   
    $logins = array(
        'user' => 'pass',
        'user1' => 'pass1',
        'user2' => 'pass2',
        'user3' => 'pass3',
        'user4' => 'pass4'
    );

    // Clean up the input values 
    foreach($_POST as $key => $value) {  
        $_POST[$key] = stripslashes($_POST[$key]); 

        $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key])); 
    }

    /******************************************************************************/
       if (isset($_POST['submit'])){

          $user = isset($_POST['user']) ? strtolower($_POST['user']) : '';
          $pass = isset($_POST['pass']) ? $_POST['pass'] : '';
          $report = $_POST['typereport'];

          if ((!array_key_exists($user, $logins))||($logins[$user] != $pass)) {
             showForm("Wrong Username/Password");
             exit();     
          }
          else {
    if ($report == "Clinical") {    
?>
<html>
<head>
</head>

<body>
CLINICAL PAGE
</body>
</html>
<?php
    }
    elseif ($report == "Annual Education") {
?>
<html>
<head>
</head>

<body>
ANNUAL EDUCATION PAGE
</body>
</html>
<?php
    }
          }
       } else {
          showForm();
          exit();
       }

    function showForm($error=""){
    ?>
    <!DOCTYPE html>
    <html><head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title>Certificate Printing :: Login</title>
    <script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
    <Script>
    $(function() {
      $("#user").focus();
    });
    </Script>
    </head>

    <body>

    <form id="login" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd">
        <h1>Log In</h1>
        <fieldset id="inputs">
            <input id="user" name="user" placeholder="Username" autofocus="" required="" type="text">   
            <input id="pass" name="pass" placeholder="Password" required="" type="password">
        </fieldset>
        <fieldset id="actions">
            <input type="radio" name="typereport" id="cbox" value="Clinical" checked="yes" /> Clinical 
            <input type="radio" name="typereport" id="cbox" value="Annual Education" /> Annual Education
        </fieldset>
        <fieldset id="actions">
            <input id="submit" name="submit" value="Log in" type="submit">
        </fieldset>
        <div class="caption"><?php echo $error; ?></div>
    </form>
    </body>
    </html>
    <?php   
    }
    ?>

对于printCert.phpprintCertHR.php ,第一行是:

<?php require_once('printExam.php'); ?>

假定用户每次访问任一页面时都调用printExam.php页面。 如果用户名和密码匹配,并且取决于选择(无论是临床还是年度教育),则应将用户带到正确的页面。 我知道表单可以正常工作,因为如果我输入了错误的用户名/密码,它会向我显示错误,但一旦正确,它就不会重定向。 知道如何解决吗?

请注意 :用户名/密码仅用于示例!

PHP中的“ Location” 标头命令应后跟exit;

否则,将执行以下代码,即,将任何输出发送到浏览器。

请参阅PHP标头页面中的示例

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

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

位置(或任何其他标头 )必须是脚本所回显的第一件事。 删除脚本中的空白行。

在调用标题之前,您已经显示了内容(关于您对Rob W的评论)。 解决该问题的方法是放入ob_start(); 在您的php代码的顶部,以及ob_end_flush(); 在代码末尾,因此可以在代码之间的任何位置调用标头。

elseif ($report == "Annual Education") { ,尝试print_r($report) -确保它是您所期望的...

if ($report == "Clinical") {
    header ("Location: printCert.php");
}
elseif ($report == "Annual Education") {
    header ("Location: printCertHR.php");
}
print_r($report);

另外,尝试监视FireBug的NET标签(或CHROME的)

是否可能需要使用PHP的输出缓冲区来解决此问题? 解析脚本后,将函数的表单输出丢掉了,因为该函数将在脚本其余部分运行之前进行解析。

另外,您应该使用strcmp来比较字符串,而不是==符号。 strcmp与==

尝试使用输出缓冲区功能,看看是否可以解决。 它看起来像这样:

function showForm($error=""){
ob_start(); ?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Certificate Printing :: Login</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
<Script>
$(function() {
  $("#user").focus();
});
</Script>
</head>

<body>

<form id="login" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd">
    <h1>Log In</h1>
    <fieldset id="inputs">
        <input id="user" name="user" placeholder="Username" autofocus="" required="" type="text">   
        <input id="pass" name="pass" placeholder="Password" required="" type="password">
    </fieldset>
    <fieldset id="actions">
        <input type="radio" name="typereport" id="cbox" value="Clinical" checked="yes" /> Clinical 
        <input type="radio" name="typereport" id="cbox" value="Annual Education" /> Annual Education
    </fieldset>
    <fieldset id="actions">
        <input id="submit" name="submit" value="Log in" type="submit">
    </fieldset>
    <div class="caption"><?php echo $error; ?></div>
</form>
</body>
</html>
<?php   
return ob_get_flush();
}

输出缓冲区 (php.net)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM