0

My website has two versions, one for desktop and one for mobile. (website.com / m.website.com).

I use a PHP script to redirect my incoming mobile traffic to the mobile version of my website. For some reason, I need to know the traffic source whithin the mobile version.

To do so I use a script to set a cookie with the traffic source name, before the mobile redirection.

However I can't access the cookie with the mobile version. It seems like its not set.

$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);

if($refData['host'] == 'www.traffic_source_1.com') {
  setcookie("traffic_source_1", 1, time() + (86400 * 30), "/");
}

// Redirect to mobile version

When I try to access the cookie within the mobile version I get a "not working" result.

 if(isset($_COOKIE["traffic_source_1"])) {
     echo "working";
  } else {
      echo "not working";
  }

UPDATE:

Replaced

setcookie("traffic_source_1", 1, time() + (86400 * 30), "/");

To

setcookie("traffic_source_1", 1, time() + (86400 * 30), "/",".website.com");

It's working with a test cookie. However when I try to include this line into my condition it doesn't. (The condition parameters are working).

casusbelli
  • 361
  • 1
  • 4
  • 19
  • 1
    you need to set the cookie path with domain name – Noman Dec 15 '16 at 10:21
  • 1
    Looks like you want to share cookie between domain and subdomain, this link may help you http://stackoverflow.com/questions/18492576/share-cookie-between-subdomain-and-domain – Param Bhat Dec 15 '16 at 10:25

1 Answers1

1

Old Code

setcookie("traffic_source_1", 1, time() + (86400 * 30), "/");

Replace WIth

setcookie("traffic_source_1", 1, time() + (86400 * 30), "/",".website.com");
Param Bhat
  • 454
  • 6
  • 17