我有一个global.php文件,我从每个脚本的顶部调用。该脚本旨在承载我所有的标题信息。PHP问题,似乎打破了我的谷歌分析脚本
发生的问题是,一旦我将google分析代码放入global.php中,它不再向我的谷歌分析注册数据。
这里是global.php代码:
<?php
/* this file needs to be included in every user-facing script that serves ads */
/* this will prevent "PHP Warning: Cannot modify header information - headers already sent.." */
ob_start();
/* this will make session cookie worl for both http://www.comehike.com and http://comehike.com
* session cookie lifetime is set to 2 hours
*/
session_set_cookie_params("7200", "/", ".comehike.com");
/* it is better to have session_start here rather then in every page
* session_start needs to be called before any headers sent to not trigger php warning */
session_start();
global $production;
$production = true;
/*============================== ad pacement utils ===============================*/
/*
* this helper module will simplify ads placement in production and simulate ads in development.
* It will allow to turn of the ads for the user with premium account.
*
* To use this module:
* in the head, where google site verification needs to be included, add the following
<?php
printGoogleSiteVerification();
?>
* in the page, where ads needs to be inserted, add the following lines (example):
<?php
printGoogleAdPlacement(7425893768, 250, 250, '10/11/10');
?>
*/
function printGoogleSiteVerification(){
global $production;
$str = '';
if($production===true){
$str.= '<meta name="google-site-verification" content="RJcu8ObHW6huUbyea336xJ1WgLiC4478fz3_3r_zthc" />'."\n";
$str.= '<meta name="alexaVerifyID" content="-2hEyu8phk6CFHF-PTqdQvP2B8k" />'."\n";
}
echo $str;
}
function hasPremium(){
return false;
/*
//this is a sample function to check is user has a premium account.
//in this example alex won't see any ads
if($_SESSION["user_id"]==='alex'){
return true;
}else{
return false;
}*/
}
//ex: printGoogleAdPlacement(7425893768, 728, 90, 10/11/10)
//ex: printGoogleAdPlacement(7425893768, 250, 250, 10/11/10)
//ex: printGoogleAdPlacement(7425893768, 200, 200, 10/11/10)
function printGoogleAdPlacement($google_ad_slot, $width, $height, $created){
global $production;
//user with premium won't see any ads.
//the question is what goes into the slots instead?
if(hasPremium() === true) {return;}
$str = '';
if($production === true){
$str.= '<script type="text/javascript"><!--'."\n";
$str.= 'google_ad_client = "pub-1884332214318974";'."\n";
$str.= '/* '.$width.'x'.$height.', created '.$created.' */'."\n";
$str.= 'google_ad_slot = "'.$google_ad_slot.'";'."\n";
$str.= 'google_ad_width = '.$width.';'."\n";
$str.= 'google_ad_height = '.$height.';'."\n";
$str.= '//-->'."\n";
$str.= '</script>'."\n";
$str.= '<script type="text/javascript"'."\n";
$str.= 'src="http://pagead2.googlesyndication.com/pagead/show_ads.js">'."\n";
$str.= '</script>'."\n";
}else{
/*
$str .= '<div style="padding:0; height:'.($height+20).'px; width:'.$width.'px;border: 1px solid #888;background:#fff;">'.
'<div>Advertisement</div>'.
'<img src="images/fake_ads/ad_'.$width.'_'.$height.'.png"></img>'.
'</div>';
*/
$str = '<img src="images/fake_ads/ad_'.$width.'_'.$height.'.png"></img>';
}
$str = '<div style="padding:0; height:'.($height).'px; width:'.$width.'px;background:#fff;">'.
$str.
'</div>';
echo $str;
}
/* =============== end of ad placement utils =================== */
/* ===================== google anaytics ========================*/
function printGoogleAnalytics()
{
$str = '';
if($production === true)
{
$str.= '<script type="text/javascript">'."\n";
$str.= '(function() {'."\n";
$str.= ' var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;'."\n";
$str.= ' ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";'."\n";
$str.= ' var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);'."\n";
$str.= '})();'."\n";
$str.= '</script>'."\n";
}
echo $str;
}
/* ===================== end of google anaytics ========================*/
?>
下面是一个例子页面,出现这种情况:
http://www.comehike.com/outdoors/hiking_headlamp.php
如果我参加了goog analitics码出global.php的并将它作为普通html放在标题中,它可以工作,因为我可以看到访问者数据。如果我把它放到global.php文件中,它不起作用。
这里是我尝试从在global.php
任何想法是怎么回事错在这里头调用该函数?如果它只是一个语法问题,它会更坏,对吧?
乌尔页面呈现的时候,看看源代码,调试 – Ibu 2011-05-28 04:09:12
@Ibu你指的是html源代码? – Genadinik 2011-05-28 04:09:49
是的html源码,看看谷歌代码是否正确加载 – Ibu 2011-05-28 04:11:31