Your open source for Development and Design

Tracking visitors with PHP and Predefined variables

Filed under: Apache,PHP — Tags: , , , — Adi @ 4:37 pm January 15, 2009

Although Google analytics can tell us a lot about who is visiting our websites it is still flawed in many ways. Google analytics is a powerful tool but if the user has disabled Javascript or you want to know live stats it can be pretty useless.

Developing our own can be much more versatile as we can gather information straight from the server using PHP’s Predefined variables.

You can add your own version of with a database and a few lines of PHP code.

$time = mktime();
if (isset($_SERVER['REQUEST_METHOD'])) {   // HTTP-method
   $method = $_SERVER['REQUEST_METHOD'];
} else {
   $method = "";
}
if (isset($_SERVER['REMOTE_ADDR'])) {      // IP-adress
   $ip_adress = $_SERVER['REMOTE_ADDR'];
} else {
   $ip_adress = "";
}
if (isset($_SERVER['HTTP_USER_AGENT'])) {  // Browser
   $browser = $_SERVER['HTTP_USER_AGENT'];
} else {
   $browser = "";
}
if (isset($_SERVER['PHP_SELF'])) {         // Current page
   $page = $_SERVER['PHP_SELF'];
} else {
   $page = "";
}
if (isset($_SERVER['HTTP_REFERER'])) {    // Previous page
   $referer = $_SERVER['HTTP_REFERER'];
} else {
   $referer = "";
}

$sql="INSERT INTO user_log (time, method, page, referer, adress, browser) // Insert into database as new row
					VALUES('$time','$method','$page','$referer','$ip_adress','$browser')";
mysql_query($sql);

SEO Apache Mod rewrite

Filed under: Apache — Tags: , , — Adi @ 3:03 pm January 12, 2009

I always tried to avoid using the .htaccess file as it either works really well or will take down the entire site. The code below will work on most sites using apache as long as they have modrewrite turned on and you edit the rules to suit your domain.
You may ready be using a .htaccess file on your server so double check as it may not show up on all FTP programs.

But why do you need to use one, well the short answer is you don’t but the long answer is it can help a lot to stop duplicate content being taken off search engines and in turn help improve your SEO.
For example how often do you see urls like www.bbc.com/index.html ?

The code below will redirect any variation on your homepage to just one. it will add the www. to the domain name and remove the index.html or index.php which ever you might be using.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^somedomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.somedomain.com/$1 [R=301,L]

RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.html(.*)\ HTTP/ [NC]
RewriteRule .* http://www.somedomain.com/ [R=301,L]

Switch to our mobile site