[英]php header location not working in script
每當我提交表單時,我都會通過打開php中的錯誤報告來遇到此錯誤:
警告:無法修改標頭信息-已在/Applications/MAMP/htdocs/crains/register.inc中發送的標頭(輸出始於/Applications/MAMP/htdocs/cranium/includes/header.inc.php:14)。第65行的php
在我的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標簽,之后沒有空格或其他任何空格)。
在我的register.inc.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行指向標題(“ Location:www.google.com。”);
有任何想法嗎? 謝謝!
在header()
調用之前,您無法輸出任何內容。 檢查您的代碼是否被回顯,或者顯示任何錯誤。
查看header.inc.php
第14行。這是輸出內容的地方。
您可以通過使用輸出緩沖區來解決此問題。
http://php.net/manual/de/function.ob-start.php
這將緩沖輸出,直到刷新它或腳本完成為止。 之前沒有輸出=>沒問題!
header.ing.php IS輸出。
您運行的瞬間包括“ include('header.inc.php')”。
解決方案:稍后運行。
另外,在您的header(location)命令之后添加“ exit”,以停止執行腳本的其余部分。 PHP將繼續運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.