Source of file Visitor.php
Size: 4,426 Bytes - Last Modified: 2021-12-23T10:06:53+00:00
/var/www/docs.ssmods.com/process/src/code/Visitor.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 | <?php class Visitor extends DataObject { private static $db = array ( 'IPAddress' => 'varchar', 'referer' => 'text', 'searchTerm' => 'varchar', 'securityID' => 'varchar', 'resolution' => 'varchar', 'platform' => 'varchar' ); private static $has_many = array ( 'PageViews' => 'PageView' ); public function logPageArrival() { $referer = isset( $_POST[ 'ref' ] ) ? $_POST[ 'ref' ] : ""; $resolution = isset( $_POST[ 'res' ] ) ? $_POST[ 'res' ] : ""; $lastPageViews = PageView::get( "PageView", "VisitorID = {$this->ID}" ); if ( $lastPageViews->count() > 0 ) { $scrollDepth = 0; $vidLength = 0; $lastPageView = $lastPageViews->last(); // Calculate the time on the previous page $startTime = strtotime( $lastPageView->Created ); $endTime = time(); $timeOnPage = $endTime - $startTime; // Check if a scroll depth for the previous page was sent through if ( isset( $_COOKIE[ "vt_sd" ] ) ) { // Get the scroll depth $scrollDepth = (int)$_COOKIE[ "vt_sd" ]; // Reset the depth setcookie("vt_sd", 0, time() - 3600); } // Check if video details were sent through if ( isset( $_COOKIE['vid-start'] ) && isset( $_COOKIE['vid-end'] ) ) { $vidLength = (int)$_COOKIE['vid-end'] - (int)$_COOKIE['vid-start']; setcookie("vid-start", "", time()-3600); setcookie("vid-end", "", time()-3600); } $lastPageView->ScrollDepth = $scrollDepth; $lastPageView->TimeOnPage = $timeOnPage; if ( $vidLength > 0 ) $lastPageView->Notes = "The video on this page was viewed for " . $vidLength . " seconds"; $lastPageView->write(); } // Save this page view (Scroll depth will be updated on the next page's view) $PageView = PageView::create( array ( 'VisitorID' => $this->ID, 'URL' => isset( $_SERVER[ 'REQUEST_URI' ] ) ? str_replace( "/handleVisitor", "", $_SERVER[ 'REQUEST_URI' ] ) : "", 'Referrer' => $referer, 'UserAgent' => isset( $_SERVER[ 'HTTP_USER_AGENT' ] ) ? $_SERVER[ 'HTTP_USER_AGENT' ] : "", 'Cookie' => isset( $_SERVER[ 'HTTP_COOKIE' ] ) ? $_SERVER[ 'HTTP_COOKIE' ] : "", 'ScrollDepth' => 0, 'Notes' => '' ) ); print_r( $PageView ); $PageView->write(); } public static function initVisitor() { $secID = SecurityToken::inst()->getSecurityID(); if ( ! $visitor = self::get()->find( 'securityID', $secID ) ) { $referer = isset( $_POST[ 'ref' ] ) ? $_POST[ 'ref' ] : ""; $resolution = isset( $_POST[ 'res' ] ) ? $_POST[ 'res' ] : ""; $platform = isset( $_POST[ 'plat' ] ) ? $_POST[ 'plat' ] : ""; $searchTerm = ""; // This is a new visitor so lets see if we can find out where they came from $visitor = self::saveVisitor( $secID, $_SERVER[ 'REMOTE_ADDR' ], $referer, $searchTerm, $resolution, $platform ); } return $visitor; } private static function getDomain( $url ) { return parse_url( $url, PHP_URL_HOST ); } private static function saveVisitor( $secID, $ipAddress, $referer = "", $searchTerm = "", $resolution = "", $platform = "") { $visitor = self::create( array ( "IPAddress" => $ipAddress, "securityID" => $secID, "referer" => $referer, "searchTerm" => $searchTerm, "resolution" => $resolution, "platform" => $platform )); $visitor->write(); return $visitor; } } |