用phpmailer发送后重定向

时间:2013-03-06 08:45:17

标签: php redirect phpmailer

我希望在成功提交表单后将用户重定向到感谢页面。我使用下面的脚本但收到错误消息。请帮忙。

<?php
session_start();

$name = check_input($_POST['name'], "Name cannot be empty.");
$email     = check_input($_POST['email'], "Email address cannot be empty.");

if(!preg_match("/^([A-Za-z\s\-]{2,45})$/i", $name))
{ 
show_error("name not valid.");
}

if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Email address not valid.");
}

htmlentities ($message, ENT_QUOTES);

require("../PHPMailer/class.phpmailer.php");

$mail = new PHPMailer();

$mail->From     = "$email";
$mail->AddAddress("myfriend@example.net");

$mail->Subject  = "An HTML Message";
$mail->Body     = "Hello, <b>my friend</b>! \n\n This message uses HTML entities!";
$mail->WordWrap = 50;

foreach(array_keys($_FILES['photo']['name']) as $key) {
$source = $_FILES['photo']['tmp_name'][$key]; // location of PHP's temporary file for this.
$filename = $_FILES['photo']['name'][$key]; // original filename from the client

$mail->AddAttachment($source, $filename);
}

/* Redirect visitor to the thank you page */
header('Location: pthankyou.php');
exit();

function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlentities($data);
if ($problem && strlen($data) == 0)
{
    show_error($problem);
}
return $data;
}

function show_error($Error)
{}
?>

错误消息

  

警告:无法修改标头信息 - 已发送的标头   (输出从PHPMailer / class.phpmailer.php:1370开始)在process.php中   在第66行

1 个答案:

答案 0 :(得分:0)

redirect()是一个特殊的php内置函数,需要在之前调用任何输出。

输出可以是:

  • 回波
  • html标签
  • var dumping
  • <?php标记
  • 之前的空格

但是,在您的代码中,我没有看到redirect()调用,因此如果您需要更好的帮助,则必须包含问题代码段