PHP重定向未工作标头已发送
所以我这里有一些代码,只使用电子邮件地址进行基于MySQL的登录。代码的这一部分工作得很好。当我尝试在验证电子邮件地址后重定向到所请求的页面时,即为“我已”。这是用于WiFi身份验证的,因此header函数将发送到一个动态字符串变量,该变量将被连接在一起。然而,当调用header函数时,它只是将我转储回主登录页 错误处理会报告臭名昭著的“已发送的邮件头”。然而,我已经搜索了我的代码,清理了代码,并在书尾前后回显、打印和空白。我在这里真的需要一些帮助,因为我还没有找到问题并开始工作();ob_end_flush();也不太管用。。。提前感谢 logon.phpPHP重定向未工作标头已发送,php,Php,所以我这里有一些代码,只使用电子邮件地址进行基于MySQL的登录。代码的这一部分工作得很好。当我尝试在验证电子邮件地址后重定向到所请求的页面时,即为“我已”。这是用于WiFi身份验证的,因此header函数将发送到一个动态字符串变量,该变量将被连接在一起。然而,当调用header函数时,它只是将我转储回主登录页 错误处理会报告臭名昭著的“已发送的邮件头”。然而,我已经搜索了我的代码,清理了代码,并在书尾前后回显、打印和空白。我在这里真的需要一些帮助,因为我还没有找到问题并开始工作();ob_en
<?php
ob_start();
//Uncomment this section for troubleshooting
/*
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
*/
//Connect to the database
include_once 'includes/db_connect.php';
//Connect the functions
include_once 'includes/functions.php';
//Start Secure Session
sec_session_start();
//Define Some Variables
$email = "";
$base_grant_url = urldecode($_GET['base_grant_url']); //get info from meraki redirect
$user_continue_url = urldecode($_GET['user_continue_url']); //get info from meraki redirect
$url = $base_grant_url.$cont.$user_continue_url.$dur;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$email = htmlspecialchars(($_POST["email"]));
$email = test_input($email);
if(!isset($errorMsg)) {
if (login($email, $dbh)) {
header("Location: $url");
exit;
}
else //email not found in db
{$errorMsg = "E-Mail address not found in database. Please retry and make sure you are using your Rent Cafe username.";}
}
}
ob_flush();
?>
functions.php
<?php
ob_start();
define("SECURE", TRUE);
function sec_session_start() {
$session_name = 'sec_session_id'; // Set a custom session name
$secure = SECURE;
// This stops JavaScript being able to access the session id.
$httponly = true;
// Forces sessions to only use cookies.
if (ini_set('session.use_only_cookies', 1) === FALSE) {
header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
exit();
}
// Gets current cookies params.
$cookieParams = session_get_cookie_params();
session_set_cookie_params($cookieParams["lifetime"],
$cookieParams["path"],
$cookieParams["domain"],
$secure,
$httponly);
// Sets the session name to the one set above.
session_name($session_name);
session_start(); // Start the PHP session
session_regenerate_id(); // regenerated the session, delete the old one.
}
function login($email, $dbh) {
$stmt = $dbh->prepare("SELECT email FROM tenants WHERE email = :email");
$stmt->bindParam(':email', $email);
$stmt->execute();
if ($stmt->rowCount() > 0)
{return true;}
else {
date_default_timezone_set('America/Los_Angeles');
$current_date = date('m/d/Y == H:i:s');
$time = time();
$stmt2 = $dbh->prepare("INSERT INTO login_attempts (time, email_id) VALUES (?, ?)");
$stmt2->bindParam(1, $current_date);
$stmt2->bindParam(2, $email);
$stmt2->execute();
return false;
}
}
function test_input($data) {
$errorMsg = "";
if (empty($data)) { //left form blank
$errorMsg = "Email is required";
return $data;
}
else {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$data))
{$errorMsg = "Invalid email format";}
return $data;
}
}
?>
db_connect.php
<?php
ob_start();
$dbhost = '****';
$dbuser = '****';
$dbpass = '****';
$db = '****';
$cont = '?continue_url=';
$dur = '&duration=1440';
try {
$dbh = new PDO("mysql:host=$dbhost;dbname=$db", $dbuser, $dbpass);
}
catch (PDOException $e) {
die();
}
?>
对于编写PHP代码,是可选的
如果我们将?>
放在文件末尾,您的web服务器可能会在其后面添加一些空格
这就是重定向没有发生的原因
只需最后删除?>
注意:这是Drupal和CodeIgniter中遵循的标准实践。在之前删除多余的空间,谢谢大家。我将在几分钟内实施这些建议。我相信之前的额外空间,所以我从脚本中删除了所有?>php标记,页面不再加载。php脚本完成后,我的logon.php脚本还包括HTML。删除?>标记后,脚本不会加载。有了它,它可以很好地装载。。。有什么想法吗?而且,我不再收到标题错误了。正在发生的是:原始URL。当脚本运行时,页面将返回到。。。所以它实际上没有重定向,也不再给我标题错误。。。
<?php
^------ single space, which is "output"
ob_start();