Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在页面视图计数器函数中使用setcookie_Php_Wordpress_Cookies - Fatal编程技术网

Php 如何在页面视图计数器函数中使用setcookie

Php 如何在页面视图计数器函数中使用setcookie,php,wordpress,cookies,Php,Wordpress,Cookies,我对页面浏览量计数器具有以下功能: function PageViews($postID) { $c_key = 'views_count'; $cookie_count = $_COOKIE['views_count_'.$postID]; $count = get_post_meta($postID, $c_key, true); if(!$cookie_count){ setcookie('views_count_'.$postID , 'view', time()+9999999

我对页面浏览量计数器具有以下功能:

function PageViews($postID) {
$c_key = 'views_count';
$cookie_count = $_COOKIE['views_count_'.$postID];
$count = get_post_meta($postID, $c_key, true);
if(!$cookie_count){
    setcookie('views_count_'.$postID , 'view', time()+999999999);
    if($count == ''){
        $count = 1;
        update_post_meta($postID, $c_key, $count);
        return $count;
    }else{
        $count++;
        update_post_meta($postID, $c_key, $count);
        return $count;
    }
}else{
    return $count;
}
}
当我转到“查看帖子”时,出现以下错误:

Warning: Cannot modify header information - headers already sent by (output started at....

我的cookie没有设置,这个函数在function.php中。如何设置此cookie?

在输出已发送到浏览器后,您正在尝试设置cookie。这是不允许的。您需要在生成任何输出之前调用此函数。

@AdamMo。您可以通过在向屏幕呈现任何输出之前调用此函数来实现这一点。您没有显示调用函数的位置,只显示了函数定义和错误。从错误消息中可以清楚地看出,在将某些输出发送到屏幕后,您正在调用此函数。