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