title: PHP: header() function not redirecting. When redirecting with HTML or JavaScript, Session information is lost
body: <p>This is a PHP/Apache question...</p>
  
  <ol>
  <li><p>I have the following code. In particular I would like to emphasize the
  following:</p>
  
  <pre><code>      // 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 */
  </code></pre></li>
  </ol>
  
  <p>which is where the problem lies. When I execute this code on localhost it works fine,
  but then when I execute it on the remote server (which is an ipage.com hosted site), I
  do not get the desired outcome. In fact, when the header("Location: $target); part runs
  I see a blank page (and no redirect). It's as though something was being output before
  the call to header(), but this is not the case, as I've checked it. So why is it not
  working?</p>
  
  <ol>
  <li>If I comment out the part that invokes header() and then exit, I am able to
  perform either an html redirect or a javascript redirect. However, when I do this
  I lose my session variable $_SESSION["jobseeker_email"]. I cannot understand why
  this happens.</li>
  </ol>
  
  <p>Any help with these issues would be greatly appreciated as I need to perform a redirect
  and still retain the session state from the former page, and all of this on a server
  (not just on localhost).</p>
  
  <pre><code>    &lt;?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-&gt;escape_string($email) . "'";
  
          $result = $mysqli-&gt;query($query);
  
          util_check_query_result($query, $result);
  
          $invalid_credentials = false;
  
          if ($result-&gt;num_rows == 0) {
  
            $invalid_credentials = true;
  
          } else {
  
            $row = $result-&gt;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-&gt;escape_string($email) . "'";
  
          $result = $mysqli-&gt;query($query);
  
          util_check_query_result($query, $result);
  
          $invalid_credentials = false;
  
          if ($result-&gt;num_rows == 0) {
  
            $invalid_credentials = true;
  
          } else {
  
            $row = $result-&gt;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 */
  
          }
  
        }
  
      ?&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
      &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
        &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
          &lt;title&gt;Work Net: Sign In&lt;/title&gt;
          &lt;link href="css/style.css" rel="stylesheet" type="text/css" /&gt;
        &lt;/head&gt;
        &lt;body&gt;
          &lt;div id="container"&gt;
            &lt;h1&gt;Work Net: Sign In&lt;/h1&gt;
            &lt;div id="content"&gt;
          &lt;ul&gt;
            &lt;li&gt;
              &lt;h2&gt;Employers&lt;/h2&gt;
              &lt;p&gt;&lt;a href="accountcreate.php?accounttype=employer"&gt;Create new employer account.&lt;/a&gt;&lt;/p&gt;
              &lt;form method="post" action="&lt;?php util_selfurl(true); ?&gt;"&gt;
                &lt;table&gt;
              &lt;tr&gt;
                &lt;td&gt;E-mail:&lt;/td&gt;
                &lt;td&gt;&lt;input type="text" name="employer_email" value="&lt;?= htmlentities(util_setvalueorblank($_POST['employer_email'])); ?&gt;" /&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                &lt;td&gt;Password:&lt;/td&gt;
                &lt;td&gt;&lt;input type="password" name="employer_password" value="&lt;?= htmlentities(util_setvalueorblank($_POST['employer_password'])); ?&gt;" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
                &lt;/table&gt;
                &lt;?php if (isset($employer_error)) echo "&lt;p style=\"color: red;\"&gt;" . htmlentities($employer_error) . "&lt;/p&gt;"; ?&gt;
                &lt;input type="hidden" name="employer_submitted" /&gt;
                &lt;input type="submit" value="Sign In" /&gt;
              &lt;/form&gt;
              &lt;p&gt;&lt;a href="forgottenpassword.php?accounttype=employer"&gt;Forgotten Employer Password.&lt;/a&gt;&lt;/p&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;h2&gt;Job Seekers&lt;/h2&gt;
              &lt;p&gt;&lt;a href="accountcreate.php?accounttype=jobseeker"&gt;Create new job seeker account.&lt;/a&gt;&lt;/p&gt;
              &lt;form method="post" action="&lt;?php util_selfurl(true); ?&gt;"&gt;
                &lt;table&gt;
              &lt;tr&gt;
                &lt;td&gt;E-mail:&lt;/td&gt;
                &lt;td&gt;&lt;input type="text" name="jobseeker_email" value="&lt;?= htmlentities(util_setvalueorblank($_POST['jobseeker_email'])); ?&gt;" /&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                &lt;td&gt;Password:&lt;/td&gt;
                &lt;td&gt;&lt;input type="password" name="jobseeker_password" value="&lt;?= htmlentities(util_setvalueorblank($_POST['jobseeker_password'])); ?&gt;" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
                &lt;/table&gt;
                &lt;?php if (isset($jobseeker_error)) echo "&lt;p style=\"color: red;\"&gt;" . htmlentities($jobseeker_error) . "&lt;/p&gt;"; ?&gt;
                &lt;input type="hidden" name="jobseeker_submitted" /&gt;
                &lt;input type="submit" value="Sign In" /&gt;
              &lt;/form&gt;
              &lt;p&gt;&lt;a href="forgottenpassword.php?accounttype=jobseeker"&gt;Forgotten Job Seeker Password.&lt;/a&gt;&lt;/p&gt;
            &lt;/li&gt;
          &lt;/ul&gt;
            &lt;/div&gt;
            &lt;div id="footer"&gt;
          &lt;p&gt;
            &lt;?php include('markup/footer.php'); ?&gt;
          &lt;/p&gt;
            &lt;/div&gt;&lt;!-- end #footer --&gt;
          &lt;/div&gt;&lt;!-- end #container --&gt;
        &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>