疑难解答“无法发送会话缓存限制器 - 标头已发送” [英] Troubleshooting "Cannot send session cache limiter - headers already sent"

查看:374
本文介绍了疑难解答“无法发送会话缓存限制器 - 标头已发送”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上实现了一个联系表单,它利用php和phpmailer类通过我的主机smtp服务器发送邮件。



当我提交我得到以下错误信息:

lockquote
警告:session_start()[function.session-start]:无法发送会话缓存限制器 - 头文件已发送

以下是我正在使用的代码的完整页面...

 <?php 
session_start();

$ name = trim($ _ POST ['name']);
$ email = $ _POST ['email'];
$ comments = $ _POST ['comments'];
$ captcha = $ _POST ['captcha'];

$ site_owners_email ='myemail.com';
$ site_owners_name ='我的名字';

if(strlen($ name)< 2){
$ error ['name'] =请输入您的姓名;
}

if(!preg_match('/ ^ [a-z0-9& \'\.\-_\ + +] + @ [a-z0-9 \ - ] + \。([a-z0-9\ - ] + \。)* + [az] {2} /是',$ email)){
$ error ['email '] =请输入一个有效的电子邮件地址;


if(strlen($ comments)< 3){
$ error ['comments'] =请留下评论;
}

if(int($ captcha)!==($ _SESSION ['randomnr2'])){
$ error ['captcha'] =CAPTCHA错误。请再试一次;
}

if(!$ error){

require_once('phpMailer / class.phpmailer.php');
$ mail = new PHPMailer();

$ mail-> From = $ email;
$ mail-> FromName = $ name;
$ mail-> Subject =联系表单;
$ mail-> AddAddress($ site_owners_email,$ site_owners_name);
$ mail-> Body = $ comments;

//邮件服务器设置

$ mail->邮件程序=smtp;
$ mail-> Host =myhost.com;
$ mail-> Port =25;
$ mail-> SMTPSecure =tls;
$ mail-> SMTPAuth = true;
$ mail->使用者名称=myname.com;
$ mail->密码=mypassword;

$ mail->发送();

echo< li class ='success'> Thank you。 $名称。 。我们已收到您的电子邮件,我们会尽快与您联系!< / li>;

}#如果没有错误,则结束
else {

$ response =(isset($ error ['name']))? <李> 中。 $ error ['name']。 < / li> \\\
:null;
$ response。=(isset($ error ['email']))? <李> 中。 $ error ['email']。 < / li> \\\
:null;
$ response。=(isset($ error ['comments']))? <李> 中。 $ error ['comments']。 < /锂> 中: 空值;
$ response。=(isset($ error ['captcha']))? <李> 中。 $ error ['captcha']。 < /锂> 中: 空值;

echo $ response;
}#如果在发送
?时出现错误,则结束>

表单 正常工作,因此php在很大程度上都很好。我通过表单发送消息,然后在收件箱中收到它。 解决方案

如果您不想将session_start )在脚本开始处,请考虑开启输出缓冲(ob_start())。


I've implemented a contact form on a website and it's utilising php and the phpmailer class to send the mails via my hosts smtp servers.

When I submit the form I get the following error message:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

Here's the full page of code I'm using ...

<?php
session_start();

    $name = trim($_POST['name']);
    $email = $_POST['email'];
    $comments = $_POST['comments'];
    $captcha = $_POST['captcha'];

    $site_owners_email = 'myemail.com'; 
    $site_owners_name = 'my name'; 

    if (strlen($name) < 2) {
        $error['name'] = "Please enter your name";  
    }

    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Please enter a valid email address"; 
    }

    if (strlen($comments) < 3) {
        $error['comments'] = "Please leave a comment";
    }

    if (int($captcha) !== ($_SESSION['randomnr2'])) { 
        $error['captcha'] = "CAPTCHA error. Please try again";
    }

    if (!$error) {

        require_once('phpMailer/class.phpmailer.php');
        $mail = new PHPMailer();

        $mail->From = $email;
        $mail->FromName = $name;
        $mail->Subject = "Contact Form";
        $mail->AddAddress($site_owners_email, $site_owners_name);
        $mail->Body = $comments;

        // Mail Server Settings

        $mail->Mailer = "smtp";
        $mail->Host = "myhost.com";
        $mail->Port = "25"; 
        $mail->SMTPSecure = "tls"; 
        $mail->SMTPAuth = true; 
        $mail->Username = "myname.com"; 
        $mail->Password = "mypassword"; 

        $mail->Send();

        echo "<li class='success'> Thank you " . $name . ". We've received your email. We'll be in touch with you as soon as we possibly can! </li>";

    } # end if no error
    else {

        $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
        $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
        $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null;
        $response .= (isset($error['captcha'])) ? "<li>" . $error['captcha'] . "</li>" : null;

        echo $response;
    } # end if there was an error sending
?>

The form is working so the php is, for the most part fine. I send a message through the form and I receive it in my inbox.

解决方案

If you don't want to put session_start() at the beginning of the script, consider turning on output buffering (ob_start()) instead.

这篇关于疑难解答“无法发送会话缓存限制器 - 标头已发送”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆