phpmailer - Cannot modify header information - headers already sent in php mailer
问题描述
i am coding token based user password verification, when user click reset button,then password verification link is send to user email account, when user click verification link in there email then,user get login with there username and password,everything is all right in my code my message also be send,but this error message also generated in my new window page. i am sending this email locally throw xampp server.
php:
<?php
session_start();
require_once("db.php");
//if user click submit button
if(isset($_POST['submit']))
{
//escape special charater in string first
$email = $conn->real_escape_string($_POST['email']);
// query to check if email already exists or not
$sql = "SELECT email FROM user WHERE email='$email'";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
$newToken = rand(100000000, 999999999);
//encrypt the token
$token = base64_encode(strrev(md5($newToken)));
//$token = $newToken;
//new register token
$sql1 = "UPDATE user SET token='$token',tokenExpire=DATE_ADD(NOW(),INTERVAL 1 HOUR) WHERE email='$email'";
if($conn->query($sql1) === TRUE)
{
//send mail
require '../phpmailer/PHPMailerAutoload.php';
if(!filter_var($email,FILTER_VALIDATE_EMAIL))
{
//SHOW ERROR MESSAGE
}
else
{
$mail = new PHPMailer(true); // Passing `true` enables exceptions
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.Gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPOptions = array(
'ssl'=>array(
'verify_peer'=>false,
'verify_peer_name'=>false,
'allow_self_signed'=>true
)
);
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'luckynath4@gmail.com'; // SMTP username
$mail->Password = 'secretpassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom("luckynath4@gmail.com","Hiring Top");
$mail->addAddress($email,"User"); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Hiring Top-Password Reset Message';
$mail->Body = '<!DOCTYPE html>
<html>
<head>
<title>Password Reset</title>
</head>
<body>
Hi,<h3>'.$_POST['email'].'</h3>
<p>We recieved a password reset request</p>
<p> In order to reset your password, please click on the link below,</p>
<p>if you did not make this request,you can ignore this mail.</p>
<a href="http://localhost/practise/job_portal_theme/verify1.php?token='.$token.'&email='.$email.'">verify your password</a>
<p>Thanks.</p><br />
<p>message from Hiring Top team.</p>
</body>
</html>';
if( $mail->send())
{
$_SESSION['checkEmail'] = true;
header("Location:forgot_password.php");
exit();
}
else
{
echo 'Mailer error: ' . $mail->ErrorInfo;
}
}
}
else
{
//If data failed to insert then show that error.
echo "Error:" .$sql.$conn->error;
}
}
else
{
//if email not found in database
$_SESSION['emailNotFoundError']=true;
header("Location:forgot_password.php");
exit();
}
$conn->close();
}
else
{
//redirect them back to forgot-password.php page if they didn't click Forgot Password button
header("Location:forgot_password.php");
exit();
}
?>
解决方案
发送标头前无输出
必须在进行任何输出之前调用发送/修改 HTTP 标头的函数。否则调用失败:
或者
尝试这个
使用window.location
而不是header
echo "<script>window.location.assign('forgot_password.php')</script>";
推荐阅读
- javascript - 检索firebase对象以绘制带有复选框的卡片,但每次复选框更改时都会绘制一张新卡片
- python - 如何从数据框列内的字典和原始数据框中提取值
- c++ - 有什么方法可以使用花括号作为函数参数而不在 C++ 中包含任何其他头文件?
- javascript - 使用 node.js 读取修改和写入日期
- android - 无法将活动范围内的对象注入 ViewModel
- reactjs - ReactJS:如何从 onClick 获取每张卡片的索引数组
- apache - 用于 profile.php 查询的 htaccess 重写器
- javascript - NextJS 构建偶尔会失败然后成功
- c# - 即使在 Outlook 中,如何附加到 BeforeAttachmentRead?
- amazon-web-services - 您是否需要在 AWS 上使用 Global Accelerator 的 NAT 网关和私有应用程序负载均衡器?