With all the fancy tracking and web analytics software out there, you’d wonder why someone would still want to have their own tracking functionality on a website. I find it easier to customize a little script than to figure out how to get it working in the analytics software. Sometimes you really just want something simple done, and don’t want to go through all the effort to figure it out.
Here is a handy little script you can use.
<?php //saves ip address and timestamp $str=date("Y-m-d H:i:s") . ": ". $_SERVER['REMOTE_ADDR'] . "\n"; file_put_contents("ip_list.txt", $str, FILE_APPEND); header("content-type: image/gif"); //43byte 1x1 transparent pixel gif echo base64_decode("R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="); ?>
What the above does is simply logs the IP address of the site visitor and the time they visited to a plan text file in the root directory. It then displays a 1×1 pixel transparent GIF file to the browser.
Why the output of a GIF file? So that you can in any of your HTML documents just put and it will call the script and display a transparent GIF. So it won’t affect the way your website is being displayed, but it will still record the visitor’s details. It also doesn’t rely on javascript, which is always a plus for me.
You can expand the script even more by changing it to include more statistics. You can make it as complex or as simple as possible.
There are many ways to get the metatags from a website, and I’ve played around with a few ideas, until I got the following function working nicely. This seems to be the most accurate and catering for all the different ways people make use of metatags. If you see a flaw, or if it’s not working for a website you’re testing it on, I’d be happy to know about it so that I can fix it.
function parsetags($url) { $contents = file_get_contents($url); $result = false; $title = null; $metaTags = null; preg_match('/<title>([^>]*)<\/title>/si', $contents, $match ); if (isset($match) && is_array($match) && count($match) > 0) { $title = strip_tags($match[1]); } preg_match_all('/<[\s]*meta[\s]*name="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match); if (isset($match) && is_array($match) && count($match) == 3) { $originals = $match[0]; $names = $match[1]; $values = $match[2]; if (count($originals) == count($names) && count($names) == count($values)) { $metaTags = array(); for ($i=0, $limiti=count($names); $i < $limiti; $i++) { $metaTags[strtolower($names[$i])] = array ( 'html' => htmlentities($originals[$i]), 'value' => $values[$i] ); } } } $result = array ( 'title' => $title, 'metaTags' => $metaTags ); return($result); }
This returns an array in the form of:
$result['title'] => 'The Title' $result['metaTags']['keywords']['value'] => 'keyword1, keyword2, keyword3' $result['metaTags']['keywords']['html'] => '<meta name="keywords" content="keyword1, keyword2, keyword3" />' - HTML version $result['metaTags']['description']['value'] => 'This is the description.' $result['metaTags']['description']['html'] => '<meta name="description" content="This is a description." />' - HTML version