【发布时间】:2012-07-08 08:48:27
【问题描述】:
这是一个 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>
【问题讨论】:
-
听起来像是 UTF8-BOM 问题。你怎么确定header之前没有输出,你收到什么headers?
-
当你看到一个空白页面并且没有重定向服务器的响应内容是什么?
-
你能显示 var_dump(util_siblingurl("jobseekermain.php", true) );的输出吗
-
var_dump 的输出是:string(59) "timescapezonecom.ipage.com/worknet/jobseekermain.php" (这与我在名为timescapezonecom.ipage.com/worknet/login.php 的文件中发布的代码不同)。
-
@JohnGoche 你启用错误报告了吗?
标签: php javascript apache redirect http-redirect