开发者

setcookie() always fails "headers already sent" even following the rules on php.net

开发者 https://www.devze.com 2023-01-12 11:43 出处:网络
The relevant peice of code is below. According to php.net I have to make sure there is no output, not even any whitespace. There isn\'t any. the php tag is the very first tag in the document no whites

The relevant peice of code is below. According to php.net I have to make sure there is no output, not even any whitespace. There isn't any. the php tag is the very first tag in the document no whitespace preceding it. What am I doing wrong?

<?php

    // main.php
    // 6:48 PM 8/6/开发者_JAVA百科2010

    include('config.php');

    // Does myid cookie exist?
    if ( !isset( $_COOKIE['myid'] ) )
    {
        // Generate myid
        $myid = substr(md5(date( 'Ymdhis' ) . str_replace( '.', '', $_SERVER['REMOTE_ADDR'] ) ), 0, 10);

        // set the cookie
        setcookie( 'myid', $myid, time() + 31536000 );


There needs to be no output at all before calling setcookie, even from other scripts.

If config.php had whitespace before the opening <?php tag (or after the closing ?>), for example, that would cause the problem.

Hunt down where header is called or some text outside of PHP tags (even whitespace) exists. That's what's happening, no doubt.


Some text editors don't show all symbols. Please try to open your main.php and config.php un different editors and check for something before <?php in both files. I can't tell about Windows, but on Linux you can use vim or vi. Also, full error message is telling you where exactly error started.


You fail to read or at least to paste this error message here. It has DETAILED explanation, what are you doing wrong.
most likely it would say output started at config.php:XXX. Pretty clear.

if it says output started at main.php:0 - this is most likely a BOM character, and you have to re-save your file without it, using your editor Save dialog or another editor.

Always read entire error message. It is not only says what something went wrong, but often explains, what certainly happened.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号