这是一个 PHP/Apache 问题...
我有以下代码。我特别想强调以下几点:
// 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 */
这就是问题所在。当我在 localhost 上执行此代码时,它工作正常,但是当我在远程服务器(这是一个 ipage.com 托管站点)上执行它时,我没有得到想要的结果。事实上,当 header("Location: $target); 部分运行时,我看到一个空白页(并且没有重定向)。就好像在调用 header() 之前输出了一些东西,但事实并非如此,因为我已经检查过了,为什么它不起作用?
- 如果我注释掉调用 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>