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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么