What Hook to Use to Execute a PHP on Header and Footer in Every Page? I have the following code that needs to be executed at the top of the page:

&lt;?php
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
?&gt;


The code above should be used in my module:

&lt;?php
/**
 * Implements hook_help.
 *
 * Displays help and module information.
 *
 * @param path 
 *   Which path of the site we're using to display help
 * @param arg 
 *   Array that holds the current path as returned from arg() function
 */
function site_visitors_help($path, $arg) {
    switch ($path) {
    case "admin/help#site_visitors":
        return '&lt;p&gt;' . t("Displays links to nodes created on this date") . '&lt;/p&gt;';
        break;
    }
}

/**
 * Implements hook_init().
 */
function site_visitors_boot() {
    site_visitors_insert();
}

/**
 * Custom visitor function. 
 * 
 * Insert IP Address, URL to the database
 * 
 * @return 
 *   A result set of the targeted posts.
 */
function site_visitors_insert(){
    $tracker = array(
        'database' =&gt; 'c1tracker',
        'username' =&gt; 'c1tracker',
        'password' =&gt; 'mypass',
        'host' =&gt; 'localhost',
        'driver' =&gt; 'mysql',
    );

    Database::addConnectionInfo('tracker', 'default', $tracker);
    db_set_active('tracker');

    //This code requires the code above to compute the start time. This will compute the total page load.
    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $finish = $time;
    $total_time = round(($finish - $start), 4);
    $t = $total_time;
    //$url = $_GET['url'];
    $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $current_ip = $_SERVER['REMOTE_ADDR'];
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $date = date("Y-m-d");
    $time =  date("h:i:s");

    function is_bot()
    {
        $botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",
        "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
        "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
        "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
        "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
        "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
        "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot",
        "Butterfly","Twitturls","Me.dium","Twiceler");

        foreach($botlist as $bot)
        {
            if(strpos($_SERVER['HTTP_USER_AGENT'], $bot) !== false)
            return true;
        }
    return false;
    }

    if (is_bot())
        $isbot = 1;
    else
        $isbot = 0;

    db_insert('ap_visits')
        -&gt;fields(array(
            'id' =&gt; NULL,
            'length' =&gt; $t,
            'url' =&gt; $url,
            'current_ip' =&gt; $current_ip,
            'user_agent' =&gt; $user_agent,
            'date_start' =&gt; date("Y-m-d H:i:s"),
            'is_bot' =&gt; $isbot,
            'is_logout' =&gt; 0
        ))
        -&gt;execute();

    db_set_active();
}


I'm wondering how can I put the first code to be called at the very top of the page and the second code (module) to be called in the footer. My purpose here is to compute the page load and put it in length variable.

BTW, I'm using drupal 7.
hook_init() is run at the beginning of the page request. So this is where you should start your timer.

hook_exit() is run at the end of most regular page requests. And it's here you should stop your timer.

I did a search on similar modules and found Arooga, maybe you can find some ideas there. Also Devel has functionality to log page execution time, slow queries etc.