Advertisement
Guest User

track_page_visit

a guest
Aug 5th, 2013
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. <?php
  2. /**
  3. * Implements hook_help.
  4. *
  5. * Displays help and module information.
  6. *
  7. * @param path
  8. * Which path of the site we're using to display help
  9. * @param arg
  10. * Array that holds the current path as returned from arg() function
  11. */
  12. function site_visitors_help($path, $arg) {
  13. switch ($path) {
  14. case "admin/help#site_visitors":
  15. return '<p>' . t("Displays links to nodes created on this date") . '</p>';
  16. break;
  17. }
  18. }
  19.  
  20. /**
  21. * Implements hook_boot().
  22. */
  23. function site_visitors_boot() {
  24. global $start;
  25.  
  26. $time = microtime();
  27. $time = explode(' ', $time);
  28. $time = $time[1] + $time[0];
  29. $start = $time;
  30. }
  31.  
  32. /**
  33. * Implements hook_exit().
  34. */
  35. function site_visitors_exit() {
  36. global $user;
  37.  
  38. if ($user->uid != 1)
  39. site_visitors_insert();
  40. }
  41.  
  42. /**
  43. * Custom visitor function.
  44. *
  45. * Insert User Agent, IP Address, and URL to the database
  46. *
  47. * @return
  48. * A result set of the targeted posts.
  49. */
  50. function site_visitors_insert(){
  51. $tracker = array(
  52. 'database' => 'c1tracker',
  53. 'username' => 'c1tracker',
  54. 'password' => 'mypass',
  55. 'host' => 'localhost',
  56. 'driver' => 'mysql',
  57. );
  58.  
  59. Database::addConnectionInfo('tracker', 'default', $tracker);
  60. db_set_active('tracker');
  61.  
  62. global $start;
  63.  
  64. //This code requires the code above to compute the start time. This will compute the total page load.
  65. $time = microtime();
  66. $time = explode(' ', $time);
  67. $time = $time[1] + $time[0];
  68. $finish = $time;
  69. $total_time = round(($finish - $start), 4);
  70. $t = $total_time;
  71. $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  72. $current_ip = $_SERVER['REMOTE_ADDR'];
  73. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  74.  
  75. function is_bot()
  76. {
  77. $botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",
  78. "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
  79. "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
  80. "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
  81. "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
  82. "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
  83. "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot",
  84. "Butterfly","Twitturls","Me.dium","Twiceler");
  85.  
  86. foreach($botlist as $bot)
  87. {
  88. if(strpos($_SERVER['HTTP_USER_AGENT'], $bot) !== false)
  89. return true;
  90. }
  91. return false;
  92. }
  93.  
  94. if (is_bot())
  95. $isbot = 1;
  96. else
  97. $isbot = 0;
  98.  
  99. db_insert('ap_visits')
  100. ->fields(array(
  101. 'id' => NULL,
  102. 'length' => $t,
  103. 'url' => $url,
  104. 'current_ip' => $current_ip,
  105. 'user_agent' => $user_agent,
  106. 'date_start' => date("Y-m-d H:i:s"),
  107. 'is_bot' => $isbot,
  108. 'is_logout' => 0
  109. ))
  110. ->execute();
  111.  
  112. db_set_active();
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement