doubaben7394 2015-06-11 10:25
浏览 39

为什么我的饼干没有设置?

We're using php to set some cookies for our users based on where they came from - some of these will be set based on the referrer URL, and some will be set based on a short query string in the URL.

We only set 2 cookies, and the purpose is to track where traffic is coming from and include the data in the user's submission for our product - then we are able to track which submissions come from which campaign.

It's a custom wordpress website, so the code is split across a few different files as follows:

header.php:

<?php
    $origin = $_SERVER['HTTP_REFERER'];
    $current = $_SERVER['PHP_SELF'];    
    $bestbefore = time() + 60 * 60 * 24 * 7;

    if (isset($_COOKIE['ccsvissource']))
    {
    }
    else
    {
        if (isset($_GET[gclid]))
        {
            setcookie('ccsvissource', 'Google', $bestbefore);
            setcookie('ccsvismedium', 'Adwords', $bestbefore);
        }
        elseif (stripos($origin, 'facebook') !== false)
        {
            setcookie('ccsvissource', 'Advertising', $bestbefore);
            setcookie('ccsvismedium', 'Facebook', $bestbefore);
        }
        elseif ($_GET[utm_medium] == "111")
        {
            setcookie('ccsvissource', 'Advertising', $bestbefore);
            setcookie('ccsvismedium', 'emailcampaign', $bestbefore);
        }
        else
        {
            setcookie('ccsvissource', $origin, $bestbefore);
            setcookie('ccsvismedium', 'Unknown', $bestbefore);
        }
?>

footer.php:

<?php 
    $mktSource = $_COOKIE['ccsvissource'];
    $mktMedium = $_COOKIE['ccsvismedium'];
?>

<p class="hide-me" id="mkt-source"><?php echo $mktSource; ?></p>
<p class="hide-me" id="mkt-medium"><?php echo $mktMedium; ?></p>

<script type="text/javascript">
    $(document).ready(function(){
        var mktSource = $('#mkt-source').text();
        var mktMedium = $('#mkt-medium').text();

        $('#hid-ms').val(mktSource);
        $('#hid-mm').val(mktMedium);
    });
</script>

The values will then get passed into the user's application form, with our lead management system doing the rest.

The problem is inconsistency - sometimes the cooks are being set, and sometimes not. Is there a certain circumstance client-side which will prevent cookies from being set?

I understand the user may have cookie tracking switched off, and have accounted for this - the volume of empty cookies is still too high so I think I'm missing something.

Please let me know if you need any further information!

Many thanks.

  • 写回答

1条回答 默认 最新

  • douyan4958 2015-06-11 11:36
    关注

    With new wordpress i've also face the same problem, my custom cookies wasn't set where my code was correct but somewhere i found that sometimes custom cookies does not work. So finally i found this working example with wp_head hook.

    Let suppose you want to set 2 different cookies like Area and currency you need this function with wp_head, i have use this in my personal projects. it working prefectly.

    add_action('wp_head', 'my_setcookie');
    function my_setcookie()
    {
        if (!empty($_REQUEST['area'])) {
    
            $area_set = ($_REQUEST['area']);
            setcookie('area', $area_set, time() + 3600, COOKIEPATH);
        }
    
        if (!empty($_REQUEST['currency'])) {
    
            $currency_request = ($_REQUEST['currency']);
            setcookie('currency_cookie', $currency_request, time() + 3600, COOKIEPATH);
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档