Skip to content

Instantly share code, notes, and snippets.

@kirkouimet
Created November 14, 2011 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kirkouimet/1363218 to your computer and use it in GitHub Desktop.
Save kirkouimet/1363218 to your computer and use it in GitHub Desktop.
PHP analytics function
function getAnalytics() {
$user = $_SERVER['HTTP_USER_AGENT'];
$browser = 'Unknown';
$platform = 'Unknown';
$version= '';
// First get the platform?
if(preg_match('/linux/i', $user)) {
$platform = 'Linux';
}
else if(preg_match('/macintosh|mac os x/i', $user)) {
$platform = 'Mac';
}
else if(preg_match('/windows|win32/i', $user)) {
$platform = 'Windows';
}
// Next get the name of the useragent yes seperately and for good reason
if(preg_match('/MSIE/i', $user) && !preg_match('/Opera/i', $user)) {
$browser = 'Internet Explorer';
$browserName = "MSIE";
}
else if(preg_match('/Firefox/i', $user)) {
$browser = 'Mozilla Firefox';
$browserName = "Firefox";
}
else if(preg_match('/Chrome/i', $user)) {
$browser = 'Google Chrome';
$browserName = "Chrome";
}
else if(preg_match('/Safari/i', $user)) {
$browser = 'Apple Safari';
$browserName = "Safari";
}
else if(preg_match('/Opera/i', $user)) {
$browser = 'Opera';
$browserName = "Opera";
}
else if(preg_match('/Netscape/i', $user)) {
$browser = 'Netscape';
$browserName = "Netscape";
}
// Finally get the correct version number
$known = array('Version', $browserName, 'other');
$pattern = '#(?<browser>' . join('|', $known).')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if(!preg_match_all($pattern, $user, $matches)) {
// We have no matching number just continue
}
// see how many we have
$i = count($matches['browser']);
if($i != 1) {
// We will have two since we are not using 'other' argument yet
// See if version is before or after the name
if(strripos($user,"Version") < strripos($user,$browserName)){
$version= $matches['version'][0];
}
else {
$version= $matches['version'][1];
}
}
else {
$version= $matches['version'][0];
}
// check if we have a number
if($version == null || $version== '') {
$version="?";
}
$user = array(
'userAgent' => $user,
'browserName' => $browser,
'browserVersion' => $version,
'platform' => $platform,
'language' => $_SERVER['HTTP_ACCEPT_LANGUAGE'],
);
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http';
$port = '';
if($_SERVER['SERVER_PORT'] != '80') {
$port = ':'.$_SERVER['SERVER_PORT'];
}
$url = $protocol.'://'.$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
return array(
'server' => array(
'ipAddress' => $_SERVER['SERVER_ADDR'],
'software' => $_SERVER['SERVER_SOFTWARE'],
),
'request' => array(
'method' => $_SERVER['REQUEST_METHOD'],
'protocol' => $protocol,
'host' => $_SERVER['SERVER_NAME'],
'port' => $_SERVER['SERVER_PORT'],
'path' => $_SERVER['REQUEST_URI'],
'queryString' => $_SERVER['QUERY_STRING'],
'url' => $protocol.'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'],
'referrer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null,
'time' => $_SERVER['REQUEST_TIME'],
),
'user' => $user,
);
}
@BasToTheMax
Copy link

Thanks for making this :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment