SET COOKIE PHP using REQUEST_URI and then output without refreshing page

242 Views Asked by At

I'm hoping one you bright sparks can help me. I have a simple PHP script that will output a different telephone number based on the fact if the URL they access contains a certain query string. This allows me to track phone calls easier when using Adwords.

So, the idea is if they access the site and the url has a query string xyz in it, it sets a cookie value with expire, and for that duration it displays a different telephone number. The problem is on the first page load the php is not reading the cookie value.

When a person visits example.com/?dynamic=keyword&abc=xyz it stores a cookie and the page should read the alternative number.

This is the code I have set in the <head>

<head>
<?php 
if(strpos($_SERVER['REQUEST_URI'], 'xyz') !== false ){
    $value = 'adwords_visit';
    setcookie("adwords_customer", $value);
    setcookie("adwords_customer", $value, time()+604800);
    setcookie("adwords_customer", $value, time()+604800, "/");
} 
?>
</head>

This is the code I use in the <body>

<body>
<?php if(isset($_COOKIE['adwords_customer']) && ($_COOKIE['adwords_customer'] == true)){ ?>
    <a onclick="return gtag_report_conversion('tel:1111');" href="tel:1111" title="Call Us">1111</a>
<?php } else { ?>
    <a href="tel:2222" title="Call Us">2222</a>
<?php } ?>
</body>

The above code works but the values only update on the page after a page refresh. Is there a way of doing this without having to use JavaScript?

0

There are 0 best solutions below