return the image if( isset($_COOKIE['linxo_attr']) || !isset($_SERVER['HTTP_REFERER'])) { returnImage(); return; } // lookup and parse the referrer's host to make sure we're on linxo.com $url_components = parse_url($_SERVER['HTTP_REFERER']); $host = $url_components['host']; $domain_pos = strripos($host,"linxo.com"); if($domain_pos === false || $domain_pos != (strlen($host) - strlen("linxo.com"))) { // not on linxo.com => return the image directly // error_log("Not on linxo.com domain"); returnImage(); return; } // lookup and parse the referrer's query string for utm_** parameters if(!isset($url_components['query'])) { // No Query string => return the image directly // error_log("No query string in referer"); returnImage(); return; } $query_params = array(); parse_str($url_components['query'], $query_params); if(! isset($query_params['utm_source']) || ! isset($query_params['utm_medium']) || ! isset($query_params['utm_campaign'])) { // At least one of the expected params is not available // => return the image // error_log("Not all three items in query string of referer [".$url_components['query']."]"); returnImage(); return; } // At this point, we have no cookie set and all the params are // in the query string of the referrer URL. // // So we can set the cookie with the value of the query string // => set the cookie linxo_attr with the encoded query string // name: "linxo_attr" // value : referrer's query string + "click_time=".time() // expire in 30 days // path : / // domain: .linxo.com $cookie_value = $url_components['query'] . '&click_time=' .time(); setcookie("linxo_attr", $cookie_value, time()+60*60*24*30, "/", ".linxo.com"); returnImage(); ?>