PHP 标头位置在脚本中不起作用


php header location not working in script

每当我提交表单时,我都会通过在php中打开错误报告而遇到此错误:

警告:无法修改标头信息 - 标头已由/Applications/MAMP/htdocs/cranium/include/header.inc.php:14 在第 65 行的/Applications/MAMP/htdocs/cranium/include/header..php inc:14 中发送

在我的header.inc.php中,我有以下代码:

<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Cranium eSolutions</title>
  <meta name="viewport" content="width=device-width">
  <link rel="stylesheet" href="/cranium/css/style.css">
  <script src="/cranium/js/libs/modernizr-2.5.3.min.js"></script>
</head>
<body>

我使用 include_once 将此代码包含在我的页面中。(第 14 行指向 body 标签,之后没有空格或任何内容)。

在我的注册中.php:

<?php
error_reporting(E_ALL); ini_set('display_errors', 'On');
$username = mysql_real_escape_string(stripcslashes($_POST['txtUser']));
//generate password
$password = generatePassword(10,5);

$email = mysql_real_escape_string(stripcslashes($_POST['txtEmail']));
//random code
$com_code = sha1(uniqid(rand()));

$fName = mysql_real_escape_string(stripcslashes($_POST['txtFname']));
$lName = mysql_real_escape_string(stripcslashes($_POST['txtLname']));
$address = mysql_real_escape_string(stripcslashes($_POST['txtAddress']));
$city = mysql_real_escape_string(stripcslashes($_POST['txtCity']));
$zip = (int)$_POST['txtZip'];
$country = $_POST['txtCountry'];
$mobile = mysql_real_escape_string(stripcslashes($_POST['txtMobileNo']));
$office = mysql_real_escape_string(stripcslashes($_POST['txtOfficeNo']));
$specialty = $_POST['txtFOS'];
$prof = $_POST['txtProf'];

//checkbox
$practiceFetch = $_POST['chkSOP'];
$n = count($practiceFetch);
$practice = '';
for ($i = 0; $i < $n; ++$i) {
    $practice .= $practiceFetch[$i].', ';
}
$practice = substr($practice,0,-2);

$coordinator = mysql_real_escape_string(stripcslashes($_POST['txtPrimaryCoord']));
$relationship = mysql_real_escape_string(stripcslashes($_POST['txtRelationship']));
$relEmail = mysql_real_escape_string(stripcslashes($_POST['txtRelEmail']));
$feedback = mysql_real_escape_string(stripcslashes($_POST['txtHow']));
//service concat
//if ($_POST['txtSubService']) $subService = mysql_real_escape_string(stripcslashes(' - '.$_POST['txtSubService']));
//$service = mysql_real_escape_string(stripcslashes($_POST['txtService'])).$subService;
$service = 'tae';

//start Query
require_once('../includes/db.config.php');
$checkQuery = "SELECT username FROM tblAccounts WHERE username='$username'";
$checkResult = mysqli_query($cxn,$checkQuery);
if (mysqli_num_rows($checkResult) <= 0) {
    $query = "INSERT INTO tblAccounts VALUES('','$username','$password','$fName','$lName','$address','$email','$com_code','$city',$zip,'$country',$mobile,$office,'$specialty','$prof','$practice','$coordinator','$relationship','$relEmail','$feedback','$service')";
    $result = mysqli_query($cxn,$query);
    //email confirmation
    $to = $email;
    $subject = "Confirmation from Cranium eSolutions Inc., to $username";
    $header = "Cranium eSolutions Inc.: Confirmation from craniumesolutions.com";
    $message = "Please click the link below to verify and activate your account. <br />";
    $message .= "http://imageworkz.asia/Cranium-Trial/confirm.php?passkey=$com_code";
    $sentmail = mail($to, $subject, $message, $header);
    if($sentmail) {
        header('Location: http://www.google.com');
    } else {
        header('Location: http://localhost/cranium/verificationfail.html');
    }
} else {
    echo '<span style="color:red;">Username already exists</span>';
}
?>

第 65 行指向标题("位置:www.google.com.");

有什么想法吗?谢谢!

调用之前header()不能输出任何内容。检查您的代码是否正在回显某些内容,或者是否显示任何错误。

请看header.inc.php第 14 行。这就是输出某些内容的地方。

您可以使用输出缓冲区来解决此问题。

http://php.net/manual/de/function.ob-start.php

这将缓冲输出,直到您刷新它或脚本完成。之前没有输出=>没问题!

header.ing.php 是输出。

您运行"include('header.inc.php')的那一瞬间,您有输出。

解决方案:稍后运行。


此外,在 header(location) 命令后添加"exit"以停止执行脚本的其余部分。PHP 将继续运行。