Troubleshooting “Cannot send session cache limiter

2019-08-11 22:57发布

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.

标签: php forms
6条回答
The star\"
2楼-- · 2019-08-11 23:06

There are three things to check for:

  1. As others have suggested, double check for white space at the beginning or end of the file.
  2. If your form processor has been included by another script, check the script that included it to make sure that there's no white space on THAT one.
  3. Lastly, bust out a hex editor and check for a Byte Order Mark at the beginning of the file.

That last one requires some more explanation.

Text editors sometimes add a Byte Order Mark (BOM) to files encoded using Unicode. For example, the UTF-8 BOM is , and appears as the first three characters in the file. Its purpose is to tell programs which order to read multi-byte characters in. In UTF-8, it's rarely actually needed since most UTF-8 character codes are only one byte long.

Since the BOM is intended for use by programs, not directly by humans, most text editors will silently suppress it. For example, the program SciTE has a habit of adding a BOM to UTF-8 encoded text files, and then not showing it. But it's still there, and it gets send before ANYTHING else in your file.

And that will trip your HEADERS SENT warning. So, load up a hex editor. If you're developing on Windows, you might try XVI32. On Linux, try shed (command line), ghex (gnome), or hexedit (generic X-Windows). The hex editor will show you the exact file, including any BOM there might be.

查看更多
Emotional °昔
3楼-- · 2019-08-11 23:15

I had the same problem. I added these lines at the top and it worked

if($_REQUEST['callback']){
    header('Content-Type: application/javascript');
}else{
    header('Content-Type: application/json');
}
查看更多
【Aperson】
4楼-- · 2019-08-11 23:17

"Headers already sent" means that you have done some output before you called session_start() which is a function which modifies the header. Often this is because some space in front of the first php-tag which counts as output.

查看更多
疯言疯语
5楼-- · 2019-08-11 23:22

This can often be caused by include()ed files having a new line at the end, like:

<?php

?>
[[NEW LINE!]]

Nothing - not even a space - can be outputted to the browser before session_start is called.

查看更多
Animai°情兽
6楼-- · 2019-08-11 23:24

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

查看更多
狗以群分
7楼-- · 2019-08-11 23:26

Generally this error arise when we send header after echoing or printing. If this error arise on a specific page then make sure that page is not echoing anything before calling to start_session().

Example of Unpredictable Error:

 <?php //a white-space before <?php also send for output and arise error
session_start();
session_regenerate_id();

//your page content
查看更多
登录 后发表回答