douhuang3740 2018-02-14 13:17
浏览 20
已采纳

使用带有IF和ELSE PHP的Cookie Expire,echo不能用于ELSE

Why?

I'm attempting to setup an Adword Campaign with my WordPress website, pretty easy stuff but I want to be able to SWITCH contact forms depending if they visited the site using AdWords or Bing/Google SERPS.

So the idea is that if they visit https://example.com/landing-page/ they will reach a page with different contact numbers and email form that has a title indicating that they have come from Adwords, all straight forward, but then, if they click the menu bar away from the landing page, they will get standard numbers and standard email forms, which makes the tracking process a little bit harder.

Setting the temporary cookie

So by using custom WordPress page template files, and when a visitor visits the landing page, it sets a cookie using:

<?php
    // 60 Seconds, live environment set to 6000 (1 hour)
    $date_of_expiry = time() + 60 ;
    setcookie( "adwords-customer", "adwords-visit", $date_of_expiry ); 
?>

Checking the cookie and do A or B

Then throughout the rest of the website (not present on the landing page) it will check if the cookie is present, and if present it does A, if not it does B, here's the code:

<?php 
    if(!isset($_COOKIE['adwords-customer']) || ($_COOKIE['adwords-customer'] != 'true')){
        echo "cookie set";
    } else {
        echo "cookie not set";
    }
?>

The Problem

The results are always "cookie set" and never else echo "cookie not set", thanks for any help in advance.

  • 写回答

1条回答 默认 最新

  • douluchuo0801 2018-02-14 13:21
    关注

    You see the ! here?

    if(!isset($_COOKIE['adwords-customer']) || ($_COOKIE['adwords-customer'] != 'true')){
       ^
        echo "cookie set";
    } else {
        echo "cookie not set";
    }
    

    It means that you're checking if it is NOT set, so that should be removed or invert the echos.

    And the != 'true' make sure you're not checking for boolean. If so, remove the quotes.


    Try giving this code a go!

    if(isset($_COOKIE['adwords-customer']) && ($_COOKIE['adwords-customer'] == true)){
        echo "cookie set";
    } else {
        echo "cookie not set";
    }
    

    If you find it now works but only on the URL that sets the cookie then ensure that your setcookie is set using / to indicate the entire domain, rather than just the /path/, e.g:

    $value = 'adwords-visit';
    
    setcookie("adwords-customer", $value);
    setcookie("adwords-customer", $value, time()+60);
    setcookie("adwords-customer", $value, time()+60, "/");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置