PHP: header() 函数不重定向.使用 HTML 或 JavaScript 重定向时,会话信息会丢失


PHP: header() function not redirecting. When redirecting with HTML or JavaScript, Session information is lost

这是一个PHP/Apache问题...

  1. 我有以下代码。我要特别强调以后:

          // store email in session variable
          $_SESSION["jobseeker_email"] = $_POST["jobseeker_email"];
          // perform redirect
          $target = util_siblingurl("jobseekermain.php", true);
          header("Location: " . $target);
          exit; /* ensure code below does not executed when we redirect */
    

这就是问题所在。当我在本地主机上执行此代码时,它工作正常,但是当我在远程服务器(这是一个 ipage.com 托管站点)上执行它时,我没有得到预期的结果。事实上,当标题("位置:$target);零件运行我看到一个空白页面(没有重定向)。就好像以前有东西被输出一样对 header() 的调用,但事实并非如此,因为我已经检查过了。那为什么不是加工?

  1. 如果我注释掉调用 header() 然后退出的部分,我能够执行 HTML 重定向或 JavaScript 重定向。但是,当我这样做时我丢失了会话变量 $_SESSION["jobseeker_email"]。我不明白为什么这种情况发生了。
这些问题

的任何帮助将不胜感激,因为我需要执行重定向并且仍然保留前一页的会话状态,所有这些都在服务器上(不仅仅是在本地主机上)。

    <?php
      session_start();
      require_once('include/connect.php');
      require_once('include/util.php');
      util_ensure_secure();
      if (isset($_GET['logout'])) {
        session_destroy();
        // restart session
        header("Location: " . util_selfurl(true));
      }
      function do_match_passwords($password1, $password2) {
        return strcmp($password1, $password2) == 0;
      }
      function valid_employer_login($email, $password) {
        global $mysqli;
        global $employer_error;
        $query = "SELECT passwd FROM Employer WHERE email = '" . $mysqli->escape_string($email) . "'";
        $result = $mysqli->query($query);
        util_check_query_result($query, $result);
        $invalid_credentials = false;
        if ($result->num_rows == 0) {
          $invalid_credentials = true;
        } else {
          $row = $result->fetch_assoc();
          $retrieved_password = $row["passwd"];
          if (!do_match_passwords($password, $retrieved_password))
        $invalid_credentials = true;
        }
        if ($invalid_credentials) {
          $employer_error = "Invalid credentials.";
          return false;
        }
        return true;
      }
      function valid_jobseeker_login($email, $password) {
        global $mysqli;
        global $jobseeker_error;
        $query = "SELECT passwd FROM JobSeeker WHERE email = '" . $mysqli->escape_string($email) . "'";
        $result = $mysqli->query($query);
        util_check_query_result($query, $result);
        $invalid_credentials = false;
        if ($result->num_rows == 0) {
          $invalid_credentials = true;
        } else {
          $row = $result->fetch_assoc();
          $retrieved_password = $row["passwd"];
          if (!do_match_passwords($password, $retrieved_password))
        $invalid_credentials = true;
        }
        if ($invalid_credentials) {
          $jobseeker_error = "Invalid credentials.";
          return false;
        }
        return true;
      }
      if (isset($_POST["employer_submitted"])) {
        global $error;
        // check whether specified username and password have been entered correctly
        if (valid_employer_login($_POST["employer_email"], $_POST["employer_password"])) {
          // store email in session variable
          $_SESSION["employer_email"] = $_POST["employer_email"];
          // perform redirect
          $target = util_siblingurl("jobseekermain.php", true);
          header("Location: " . $target);
          exit; /* ensure code below does not executed when we redirect */
        }
      }
      if (isset($_POST["jobseeker_submitted"])) {
        global $error;
        // check whether specified username and password have been entered correctly
        if (valid_jobseeker_login($_POST["jobseeker_email"], $_POST["jobseeker_password"])) {
          // store email in session variable
          $_SESSION["jobseeker_email"] = $_POST["jobseeker_email"];
          // perform redirect
          $target = util_siblingurl("jobseekermain.php", true);
          header("Location: " . $target);
          exit; /* ensure code below does not executed when we redirect */
        }
      }
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Work Net: Sign In</title>
        <link href="css/style.css" rel="stylesheet" type="text/css" />
      </head>
      <body>
        <div id="container">
          <h1>Work Net: Sign In</h1>
          <div id="content">
        <ul>
          <li>
            <h2>Employers</h2>
            <p><a href="accountcreate.php?accounttype=employer">Create new employer account.</a></p>
            <form method="post" action="<?php util_selfurl(true); ?>">
              <table>
            <tr>
              <td>E-mail:</td>
              <td><input type="text" name="employer_email" value="<?= htmlentities(util_setvalueorblank($_POST['employer_email'])); ?>" />
            </tr>
            <tr>
              <td>Password:</td>
              <td><input type="password" name="employer_password" value="<?= htmlentities(util_setvalueorblank($_POST['employer_password'])); ?>" /></td>
            </tr>
              </table>
              <?php if (isset($employer_error)) echo "<p style='"color: red;'">" . htmlentities($employer_error) . "</p>"; ?>
              <input type="hidden" name="employer_submitted" />
              <input type="submit" value="Sign In" />
            </form>
            <p><a href="forgottenpassword.php?accounttype=employer">Forgotten Employer Password.</a></p>
          </li>
          <li>
            <h2>Job Seekers</h2>
            <p><a href="accountcreate.php?accounttype=jobseeker">Create new job seeker account.</a></p>
            <form method="post" action="<?php util_selfurl(true); ?>">
              <table>
            <tr>
              <td>E-mail:</td>
              <td><input type="text" name="jobseeker_email" value="<?= htmlentities(util_setvalueorblank($_POST['jobseeker_email'])); ?>" />
            </tr>
            <tr>
              <td>Password:</td>
              <td><input type="password" name="jobseeker_password" value="<?= htmlentities(util_setvalueorblank($_POST['jobseeker_password'])); ?>" /></td>
            </tr>
              </table>
              <?php if (isset($jobseeker_error)) echo "<p style='"color: red;'">" . htmlentities($jobseeker_error) . "</p>"; ?>
              <input type="hidden" name="jobseeker_submitted" />
              <input type="submit" value="Sign In" />
            </form>
            <p><a href="forgottenpassword.php?accounttype=jobseeker">Forgotten Job Seeker Password.</a></p>
          </li>
        </ul>
          </div>
          <div id="footer">
        <p>
          <?php include('markup/footer.php'); ?>
        </p>
          </div><!-- end #footer -->
        </div><!-- end #container -->
      </body>
    </html>

没有看到 util_siblingurl() 的代码,我猜你的问题是路径问题。util_siblingurl() 函数和 Apache 设置的组合可能会导致路径不一致。

例如,在本地主机上,您可能会被重定向到 http://example.com/some/path/to/jobseekermain.php,而在远程主机上,您可能会被重定向到 http://example.com/some/different/path/to/jobseekermain.php

您看到的是白屏而不是 404 的事实让我在这个假设中有些犹豫,但查看该函数的代码仍然会有所帮助。

我可能会做两件事来找出问题所在:

  1. 使用Wireshark。准确查看您收到的标头。这可能会告诉您导致问题的确切原因(也许主机总是在页面顶部打印一些内容,就像一些免费主机一样?
  2. 检查其他标头重定向是否有效。创建一个 php 脚本,它只是执行重定向,其中有硬编码的值,并查看它是否有效。如果是这样,则意味着代码中的某些内容要么对页面进行某些打印,要么由于某种原因未成功构建目标 URL。

下面是一个示例脚本:

<?php
    session_start();
    // I added the require just to make sure nothing is being printed there
    // (an error for example)
    require_once('include/connect.php');
    require_once('include/util.php');
    header("Location: " . SOME_LOCATION);
    exit;
?>

当然,还有一些网页在重定向到它时打印正常......

我相信

你的代码没有问题。实际上,像ipage这样的托管系统会导致一些问题。我也使用过ipage托管计划并面临同样的问题。

所以伙计,尝试找到另一个比这些更好的托管服务器。

谢谢

我遇到了同样的问题。Ipage托管,重定向不起作用,服务器报告标头已发送。原来问题有空格。

确保 <?php code ?> 标记之外没有空格或任何其他字符。手动选择并删除重定向之前包含的所有文件中开始标记之前和结束标记之后的所有内容。