Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5958799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:35:39+00:00 2026-05-22T18:35:39+00:00

I have a global.php file that I call from the top of every script.

  • 0

I have a global.php file that I call from the top of every script. That script is meant to host all my header information.

The problem that is happening is that once I place the google analytics code into the global.php, it no longer registers data to my google analytics.

Here is the global.php code:

<?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 ========================*/
?>

Here is an example page where this happens:

http://www.comehike.com/outdoors/hiking_headlamp.php

If I take the goog analitics code out of the global.php and place it in the header as normal html, it does work because I can see the visitor data. If I put it into the global.php file, it doesn’t work.

And here is the function I try to call from the header that is in the global.php

Any idea what is going wrong here? If it was just a syntax issue it would be much more broken, right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:35:40+00:00Added an answer on May 22, 2026 at 6:35 pm

    I looked at the code and i can’t see anything wrong, the code properly create the script tag and add it in the head of your document.

    However recently i had a similar problem when trying to validate my page on google to find their script tag. My first solution was similar to yours which is pasting the code directly in the html, however this was not what i wanted since i wanted it to be automatically included in all pages so here is what i did instead.

    Rewriting the code in php seems to break it for some reason. so instead of rewriting in line per line you can write the whole thing as one string using HEREDOC syntax:

    $str =<<<GOOGLECODE
    <script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'XX-XXXXXXX-XX']);
    _gaq.push(['_trackPageview']);
    
       (function() {
       var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;
       ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
       var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);
       })();
    </script>
    GOOGLECODE;
    

    Then you can echo your $str.

    echo $str;
    

    also since you are using output buffer you can also use this method

    function printGoogleAnalytics()
    {
    ?>
    <script type="text/javascript">
        ....
    </script>
    <?php
    }
    ...
    

    calling the script will print the html in between.

    I hope that help fix your problem

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a written a script in PHP that reads from data from a
How can defined global variable ($begin) that will be recognized in all php file?
I have some global variables in a Python script. Some functions in that script
I have a class Application that my global.asax inherits from. The class has this
I have a seperate PHP file I include that has a several important functions
I'm having trouble with global variables in php. I have a $screen var set
I have PHP configured so that magic quotes are on and register globals are
I have used a static global variable and a static volatile variable in file
I have a big list of global variables that each have their own setup
I have a section of code like the following: ---- file.php ---- require_once(mylib.php); function($a,$b)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.