shimmy
shimmy

Reputation: 47

PHP, Fire cookies on condition of pages

I'm having some trouble firing cookies on the condition of what page the user visits first.

Code below fires a cookie if on pages 2641, 2998, 2949 and no cookie exists. However, how do I do it to fire a different cookie if user is on any other page on the website if no cokkies exist?

Rule: Two cookies cannot exist. Just one or the other.

Any help much appreciated :)

if (is_page([2641,2998,2949]) && !isset($_COOKIE['ppc_campaign']) && !isset($_COOKIE['organic'])) {

    $ppc_cookie = "ppc_campaign";

    $ppc_value = (!empty($_SERVER['HTTPS']))
    ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] 
    : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

    $path = "/";

    setcookie($ppc_cookie, strstr($ppc_value, '?'), time() + (86400 * 28), $path);

    $acf_applicationLink = $ppc_value;
    }   

else {

}

Upvotes: 0

Views: 38

Answers (1)

William_Wilson
William_Wilson

Reputation: 1264

Sounds like this is what you want. Check for cookie existence. If neither exists, check the specific page, otherwise do something else.

if (!(isset($_COOKIE['ppc_campaign']) || isset($_COOKIE['organic']))) {
    if (is_page([2641,2998,2949])) {

        $ppc_cookie = "ppc_campaign";

        $ppc_value = (!empty($_SERVER['HTTPS']))
        ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] 
        : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

        $path = "/";

        setcookie($ppc_cookie, strstr($ppc_value, '?'), time() + (86400 * 28), $path);

        $acf_applicationLink = $ppc_value;
    }   

    else {
        $organic_cookie = "organic";

        $organic_value = "?campaign=_ORGANIC_";

        $path = "/";

        setcookie($organic_cookie, $organic_value, time() + (86400 * 28), $path);

        $acf_applicationLink = $organic_value;
    }
}

Upvotes: 1

Related Questions