Sometimes when we do the development of the program, want to know how long the user in the Site page browsing, then how to achieve this function? This article mainly introduces the PHP record page dwell time method, involving PHP with JS for file and time related operation skills, need friends can refer to the next , specifically as follows:
First, in the page to be monitored add JS as follows
<script>var dt1 = new Date (), window.onbeforeunload = function () { var dt2 = new Date (); var ms = Dt2.gettime ()-dt1.gettime (); var img = new Image (); IMG.SRC = ' log.php?stay_ms= ' + ms;} </script>
The log.php code is as follows
<?php$refer = "if (isset ($_server[' http_referer '])) {$refer = $_server[' Http_referer '];} $user _agent = ", if (isset ($_server[' http_user_agent ')) {$user _agent = $_server[' http_user_agent '];} $ip = $_server[' remote_addr '];if (isset ($_get[' Stay_ms ')) {$log = ' ['. Date ("Y-m-d h:i:s"). '] ' . $ip. ' ' . $refer. ' @ ' . Number_format ($_get[' Stay_ms '). "Ms\r\n"; File_put_contents ("Log/log_". Date ("y-m-d"). ". txt", $log, File_append);} if ($_server[' query_string ' = = "' | | isset ($_get[' Day '])) {$day = Isset ($_get[' Day ')]: Date (" $_get["); $file = "Log/log_". $day. ". txt"; if (file_exists ($file)) {$log = file_get_contents ($file); echo nl2br ($log); }}