我正在编写一个脚本,它将每个访问者的引用 URL 保存到会话 var,然后将其与其他数据一起存储到数据库中,例如将访问者带到网站的谷歌关键字/短语以及引用链接是常规的谷歌列表(又名有机结果)或付费链接(谷歌广告词)。到目前为止,我已经想出了这个,但它并不总是有效。进行更多阅读后,我发现在几种情况下 http_referer 被阻止或隐藏,因此我正在寻找一种更好的方法来使其正常工作。
$session = & JFactory::getSession();
if (!$session->get('referrer', $origref, 'extref')) //If does not exist
{
$origref = $_SERVER['HTTP_REFERER'];
$session->set('referrer', $origref, 'extref');
$url = $session->get('referrer', $origref, 'extref');
if(!$url && !$url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : false) {
$q = 'No referer';
}
$parts_url = parse_url($url);
$query = isset($parts_url['query']) ? $parts_url['query'] : (isset($parts_url['fragment']) ? $parts_url['fragment'] : '');
if(!$query) {
$q = 'Not from Google';
}
parse_str($query, $parts_query);
$q = isset($parts_query['q']) ? $parts_query['q'] : isset($parts_query['oq']) ? $parts_query['oq'] : (isset($parts_query['p']) ? $parts_query['p'] : 'could not fetch keyword');
if(stristr($origref, 'aclk')) { // if referer is a google adwords link as opposed to an organic link
$type = ', paid link, '.$_SERVER['HTTP_USER_AGENT'];
} else {
$type = ', organic result, '.$_SERVER['HTTP_USER_AGENT'];
}
$ginfo = $q.$type;
$session->set('referrer', $ginfo, 'extref');
}
function search_engine_query_string($url = false) {
if(!$url && !$url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : false) {
return 'No referer';
}
$parts_url = parse_url($url);
$query = isset($parts_url['query']) ? $parts_url['query'] : (isset($parts_url['fragment']) ? $parts_url['fragment'] : '');
if(!$query) {
return 'Not from Google';
}
parse_str($query, $parts_query);
return isset($parts_query['q']) ? $parts_query['q'] : (isset($parts_query['p']) ? $parts_query['p'] : '');
}
有任何想法吗?