How to track the activities of visitor

Here in this post, you will learn how to track the activities of the visitor in your website. By tracking activities here, I mean to say, we will want to store information about from which link the visitor came, whats the IP address of the visitor, did he come by clicking a link on any other website or did he came directly to our page. All such information can be tracked. Information we would like to track are:


  • IP of the visitor: $_SERVER['REMOTE_ADDR']
  • Requested URI: $_SERVER['REQUEST_URI']
  • Current Website: $_SERVER['HTTP_HOST']
  • Current URL: curlPageURL()
     public function curPageURL() {
            $pageURL = 'http';
            if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
            $pageURL .= "://";
            if ($_SERVER["SERVER_PORT"] != "80") {
                $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
            } else {
                $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
            }
            return $pageURL;
        }

  • Browser: detect($_SERVER['HTTP_USER_AGENT'])
public static function detect($userAgent)     {        strtolower($userAgent);        if (preg_match('/opera/', $userAgent)) {            $name = 'opera';        }        elseif (preg_match('/chrome/', $userAgent)) {            $name = 'chrome';            }        elseif (preg_match('/webkit/', $userAgent)) {            $name = 'safari';        }        elseif (preg_match('/msie/', $userAgent)) {            $name = 'msie';        }        elseif (preg_match('/mozilla/', $userAgent) && !preg_match('/compatible/', $userAgent)) {            $name = 'mozilla';        }        else {            $name = 'unrecognized';        }        // version?        if (preg_match('/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches)) {            $version = $matches[1];        }        else {            $version = 'unknown';        }        // platform?        if (preg_match('/linux/', $userAgent)) {            $platform = 'linux';        }        elseif (preg_match('/macintosh|mac os x/', $userAgent)) {            $platform = 'mac';        }        elseif (preg_match('/windows|win32/', $userAgent)) {            $platform = 'windows';        }        else {            $platform = 'unrecognized';        }        return array(            'name'      => $name,            'version'   => $version,            'platform'  => $platform,            'userAgent' => $userAgent        );    }
Now that we can get these information we can store these information to database for each page load. This will make our Activity Tracker for our website.  

Comments

Popular posts from this blog

Automate file upload in Selenium IDE

How To Install and Configure Nextcloud