-1

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Simon Hayter
  • 3,131
  • 27
  • 53

1 Answers1

2

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, "/");
Simon Hayter
  • 3,131
  • 27
  • 53
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    This is why I posted as a wiki. – Funk Forty Niner Feb 14 '18 at 13:23
  • Hi Funk and thank you, tried the code still get the same outcome, always cookie set, even in a fresh browser with no cookies set. – Simon Hayter Feb 14 '18 at 13:26
  • Use php's error reporting then @SimonHayter and/or the Wordpress method. Make sure you're not victim of caching neither. – Funk Forty Niner Feb 14 '18 at 13:27
  • the browser I'm using is EDGE which I've never visited the landing page on but I have looked in debugger, nothing in cookies. I have enabled php errors using .htaccess and can confirm, nothing is being reported, I purposely used `` to double check that reporting is enabled and log, which it is... sadly nothing is erroring with the code that I'm using. – Simon Hayter Feb 14 '18 at 14:28
  • did you either flip the echoes, or revert the condition? the answer is not the code that you should use - he is just explaining what is wrong with it. – Stender Feb 14 '18 at 14:30
  • @SimonHayter Some browsers/OS's are problemsome with caching. See if you can go into your privacy settings and try and find a possible still residing cookie. If there are any, delete them and start over. I had that problem before. Use `var_dump()` to see what's going in (or not) and echoing the cookie superglobal. If this is a Wordpress related issue, I won't be of much help there. – Funk Forty Niner Feb 14 '18 at 14:32
  • Don't think its a caching issue or a WordPress, since I'm not communicating with WordPress hooks at all. I've ruled out the caching issue by changing the cookie check to a non-existent cookie that has never been issued `if(isset($_COOKIE['adwords-customer2']) || ($_COOKIE['adwords-customer2'] != 'true')){` it still outputs the same cookie is set on the first echo. – Simon Hayter Feb 14 '18 at 14:35
  • However `if(isset($_COOKIE['adwords-customer']) || ($_COOKIE['adwords-customer'] == true)){` seems to work but only on the path with the landing page, so I guess I have to issue a domain with the expire? – Simon Hayter Feb 14 '18 at 14:48
  • in your updated code, you are checking if it exists OR it does not equals to a string called "True" - So if it does not exists - it does not equal true - and will therefore echo "cookie is set" - on the other hand, if it is set, you will also echo "cookie is set" - So no matter what is in the cookie, it will echo "Cookie is set". - – Stender Feb 14 '18 at 14:49
  • `||` should be `&&` – Stender Feb 14 '18 at 14:50
  • @SimonHayter Try the ^ above by Stender. – Funk Forty Niner Feb 14 '18 at 14:51
  • @Stender using `if(isset($_COOKIE['adwords-123']) && ($_COOKIE['adwords-123'] == true)){` works but only on the cookie page, I'm using `setcookie( "adwords-123", "adwords-321", $date_of_expiry ); ` to set the cookie, is the cookie set wrong? – Simon Hayter Feb 14 '18 at 14:55
  • @FunkFortyNiner works, just not on the other pages apart from the page setting the cookie. I thought the check I was doing to see if the cookie was present? not what page it was issued on? – Simon Hayter Feb 14 '18 at 14:56
  • 1
    @FunkFortyNiner Got it working in the end and hope you don't mind me editing your answer so its helpful for others than just me. – Simon Hayter Feb 14 '18 at 15:46