【问题标题】:php header location attribute not workingphp标头位置属性不起作用
【发布时间】:2010-06-17 14:11:15
【问题描述】:

尽管遵循了我可以在网上找到的所有建议,但我还是遇到了标题位置问题。邮件程序脚本发送电子邮件,但我收到与第 29 行相关的“标头已发送”错误,即 {header("Location: $thanksURL");} 并且没有重定向到确认页面。如果我将标头位置代码替换为打印确认消息的指令,它会起作用,因此服务器不喜欢标头位置代码。

代码如下:

<?php
ob_start(); 
$to = "msheath@btinternet.com" ; 
$from = $_REQUEST['email'] ; 
$name = $_REQUEST['name'] ; 
$headers = "From: $from"; 
$subject = "Request for Library Document"; 
$thanksURL = "http://www.postalhistory.org.uk/newsite/php/thankyou.php"; //confirmation page
$fields = array(); 
$fields{"name"} = "Name"; 
$fields{"address"} = "Address"; 
$fields{"email"} = "Email"; 
$fields{"tel"} = "Telephone No"; 
$fields{"author1"} = "First Author"; 
$fields{"title1"} = "First Title"; 
$fields{"author2"} = "Second Author"; 
$fields{"title2"} = "Second Title"; 
$body = "I would like to borrow the undermentioned book(s) from the PHS Library:\n\n"; foreach($fields as $a => $b){    $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
$headers2 = "From: The Librarian, Postal History Society"; 
$subject2 = "Thank you for contacting the Postal History Society"; 
$autoreply = "Thank you for your request. Somebody will get back to you as soon as possible, usually within 48 hours.";
if($from == '') {print "You have not entered an email, please go back and try again";} 
else { 
if($name == '') {print "You have not entered a name, please go back and try again";} 
else { 
$send = mail($to, $subject, $body, $headers); 
$send2 = mail($from, $subject2, $autoreply, $headers2); 
if($send) 
{header("Location: $thanksURL");} 
else 
{print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; } 
}
}
ob_end_flush()
?>

http://www.postalhistory.org.uk/newsite/php/library.php自己试试吧。

谁能指出哪里出了问题?

迈克

【问题讨论】:

  • 来自我的两个字:错误的代码格式
  • 错误信息应该被完整地复制。您可能不知道,但通常错误消息包含帮助您修复它的信息。

标签: php header


【解决方案1】:

'headers already sent' 表示您已经向浏览器发送了一些内容。这可能是某处的空白。也可能是您的文件以带有 BOM 的 UTF-8 编码,这意味着您已将 BOM 发送到浏览器

【讨论】:

    【解决方案2】:

    警告:无法修改标头信息 - 标头已由 /home/users/uks52804 中的(输出开始于 /home/users/uks52804/html/postalhistory.org.uk/newsite/php/contact.php:1)发送/html/postalhistory.org.uk/newsite/php/contact.php 第 29 行

    这意味着您在调用 ob_start() 之前有输出。 ob_start 应该是包含该代码的页面的第一条指令。

    【讨论】:

    • 我已经仔细检查了所有空白问题,但绝对没有。另外,如果您将标头位置重定向替换为打印消息的指令,您如何解释该脚本有效?
    【解决方案3】:

    不久前我遇到了同样的问题,发现在设置标题之前我正在回显一些东西。我删除了 echo 语句,还清除了一些消除问题的空格

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2013-07-28
      • 2011-04-02
      相关资源
      最近更新 更多