Custom PHP code before a site is loaded I need to execute a few lines of PHP code before a page is rendered.
Basically what I need to do is to store in a database the user's original http_referer together with an anonymous visitID, and save this visitID in a session.
So the code i need to execute is something like this:

if(!isset($_SESSION['lock'])) { // we only want this to run when the user enters the site, not on subsequent pages
   $_SESSION['lock'] = true;
   save_http_referer_in_database();
   $_SESSION['visitID'] = mysql_insert_id(); // gets the id of the last inserted row
}


I've found a few solutions but not very convincing.
UPDATE 16th Oct: I'm making a short summary here as I've edited this post many many times.


First, I tried putting the required code at the top of html.tpl.php... it works, but stops running if i enable caching. Not good. I don't want to disable caching.
I tried putting the code in the settings.php and index.php, but drupal's session aren't initialized yet in there.
As suggested here, I've tried creating a custom module implementing hook_boot, but this solution has weird problems when caching is enabled.
What happens is that the save_http_referer_in_database() function correctly runs the first time, but when the user loads another page, it runs again! All the subsequent page loads don't trigger it again though.
This post has some more informations about it.
This bug report on Drupal.org shows some other bad things that happen with sessions and caching and hook_boot()
SOLUTION: Use cookies instead of sessions.


Regarding @znerol's answer:


I wouldn't have DB problems anyway because i save visitID and referer in another database, so i manually db_connect() to my database and manually run mysql_query(). Hopefully it's not a bad thing.
How could exposing the visitID to the user create problems? The visitID is a completely anonymous ID, generated by MySql as the Auto Increment column of the table containing the http_referers. I can't imagine how someone could use this ID to do something 'bad'.
The reason why I want to do it inside my application, is that I need the visitID to "connect" certain actions on my site to the http_referer (through the visit_id foreign key). So basically i have a structure like this:


VISITS (visitID, http_referer)
EVENTS (eventID, event_description, fk_visitID)

When an event is triggered, I add it to the database by fetching the fk_visitID from the cookie (or session)

I couldn't find a way to accomplish that through AWSTATS (which is installed) or Google Analytics or other similar software.

I didn't know Piwik but thanks to the API it might be fine for what I need, right?
hook_boot runs on every page, even on cached pages. Implement it in a custom module.

Generally it's better to not require code to run on cached pages, but that's the best option you have, as opposed to hacking index.php. The primary problem I see with depending on hook_boot, is that you open your self up to a very simple denial of service attack. Anyone with curl and a shell can potentially bring your site down with much less resources when caching isn't used well.

One option to consider it relying on javascript to make a call the site via ajax. That way you can save yourself quite a bit of load on the server.