title: Is possible knowing how long an user has been on a webpage by using PHP?
body: <p>Is possible knowing how long an user has been on a webpage by using PHP? Or is possibe knowing the date and time he left?
  I have a php code and I want to add an extra feature which shows me how long an user has been on a page or the date and time he left. I don't know if it's possible. I want do add it. Thanks and sorry for my bad english.</p>
  <pre><code>&lt;?php
  date_default_timezone_set('Europe/Rome');
  $handle = fopen(&quot;log_1.txt&quot;, &quot;a&quot;);
  $rem_port = $_SERVER['REMOTE_PORT']; 
  $date = date(&quot;Y-m-d H:i:s&quot;);
  $user_agent = $_SERVER['HTTP_USER_AGENT'];
  $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
  
   function getClientIP(){       
       if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)){
              return  $_SERVER[&quot;HTTP_X_FORWARDED_FOR&quot;];  
       }else if (array_key_exists('REMOTE_ADDR', $_SERVER)) { 
              return $_SERVER[&quot;REMOTE_ADDR&quot;]; 
       }else if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {
              return $_SERVER[&quot;HTTP_CLIENT_IP&quot;]; 
       } 
  
       return '';
  }
  
  function detectDevice(){
      $userAgent = $_SERVER[&quot;HTTP_USER_AGENT&quot;];
      $devicesTypes = array(
          &quot;PC&quot; =&gt; array(&quot;msie 10&quot;, &quot;msie 9&quot;, &quot;msie 8&quot;, &quot;windows.*firefox&quot;, &quot;windows.*chrome&quot;, &quot;x11.*chrome&quot;, &quot;x11.*firefox&quot;, &quot;macintosh.*chrome&quot;, &quot;macintosh.*firefox&quot;, &quot;opera&quot;),
          &quot;Tablet&quot;   =&gt; array(&quot;tablet&quot;, &quot;android&quot;, &quot;ipad&quot;, &quot;tablet.*firefox&quot;),
          &quot;Mobile&quot;   =&gt; array(&quot;mobile &quot;, &quot;android.*mobile&quot;, &quot;iphone&quot;, &quot;ipod&quot;, &quot;opera mobi&quot;, &quot;opera mini&quot;),
          &quot;bot&quot;      =&gt; array(&quot;googlebot&quot;, &quot;mediapartners-google&quot;, &quot;adsbot-google&quot;, &quot;duckduckbot&quot;, &quot;msnbot&quot;, &quot;bingbot&quot;, &quot;ask&quot;, &quot;facebook&quot;, &quot;yahoo&quot;, &quot;addthis&quot;)
      );
      foreach($devicesTypes as $deviceType =&gt; $devices) {           
          foreach($devices as $device) {
              if(preg_match(&quot;/&quot; . $device . &quot;/i&quot;, $userAgent)) {
                  $deviceName = $deviceType;
              }
          }
      }
      return ucfirst($deviceName);
      }
   
  
  
  function getOS() { 
  
      global $user_agent;
  
      $os_platform    =   &quot;Unknown OS Platform&quot;;
  
      $os_array       =   array(
                              '/windows nt 10.0/i'    =&gt;  'Windows 10',
                              '/windows nt 6.2/i'     =&gt;  'Windows 8',
                              '/windows nt 6.1/i'     =&gt;  'Windows 7',
                              '/windows nt 6.0/i'     =&gt;  'Windows Vista',
                              '/windows nt 5.2/i'     =&gt;  'Windows Server 2003/XP x64',
                              '/windows nt 5.1/i'     =&gt;  'Windows XP',
                              '/windows xp/i'         =&gt;  'Windows XP',
                              '/windows nt 5.0/i'     =&gt;  'Windows 2000',
                              '/windows me/i'         =&gt;  'Windows ME',
                              '/win98/i'              =&gt;  'Windows 98',
                              '/win95/i'              =&gt;  'Windows 95',
                              '/win16/i'              =&gt;  'Windows 3.11',
                              '/macintosh|mac os x/i' =&gt;  'Mac OS X',
                              '/mac_powerpc/i'        =&gt;  'Mac OS 9',
                              '/linux/i'              =&gt;  'Linux',
                              '/ubuntu/i'             =&gt;  'Ubuntu',
                              '/iphone/i'             =&gt;  'iPhone',
                              '/ipod/i'               =&gt;  'iPod',
                              '/ipad/i'               =&gt;  'iPad',
                              '/android/i'            =&gt;  'Android',
                              '/blackberry/i'         =&gt;  'BlackBerry',
                              '/webos/i'              =&gt;  'Mobile'
                          );
  
      foreach ($os_array as $regex =&gt; $value) { 
  
          if (preg_match($regex, $user_agent)) {
              $os_platform    =   $value;
          }
  
      }   
  
      return $os_platform;
  
  }
  
  function getBrowser() {
  
      global $user_agent;
  
      $browser        =   &quot;Unknown Browser&quot;;
  
      $browser_array  =   array(
                              '/msie/i'       =&gt;  'Internet Explorer',
                              '/firefox/i'    =&gt;  'Firefox',
                              '/safari/i'     =&gt;  'Safari',
                              '/chrome/i'     =&gt;  'Chrome',
                              '/OPR/i'        =&gt;  'Opera',
                              '/netscape/i'   =&gt;  'Netscape',
                              '/maxthon/i'    =&gt;  'Maxthon',
                              '/konqueror/i'  =&gt;  'Konqueror',
                              '/mobile/i'     =&gt;  'Handheld Browser'
                          );
  
      foreach ($browser_array as $regex =&gt; $value) { 
  
          if (preg_match($regex, $user_agent)) {
              $browser    =   $value;
          }
  
      }
  
      return $browser;
  
  }
  
  $realip         =   getClientIP();
  $user_os        =   getOS();
  $user_browser   =   getBrowser();
  $detect_device  =   detectDevice();
  
  $device_details = &quot;IP: &quot;.$realip.&quot;\r\nPort: &quot;.$rem_port.&quot;\r\nUserAgent: &quot;.$user_agent.&quot;\r\nBrowser: &quot;.$user_browser.&quot;\r\nOperative System: &quot;.$user_os.&quot;\r\nDevice Type: &quot;.$detect_device.&quot;\r\nHost: &quot;.gethostbyaddr($realip).&quot;\r\nDate: &quot;.$date.&quot;\r\n&quot;;
  
  fwrite($handle,$device_details);
  fclose($handle);
  header ('Location: /1.txt');
  
  ?&gt;
  </code></pre>