0) { // 1. We know who customer is // Add Customer entry to Visitors if (!tep_db_num_rows(tep_db_query("select counter FROM " . TABLE_VISITORS . " where (browser_ip = '" . $b_ip . "' || customers_id = '" . (int)$customer_id . "') AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1"))) { // A1. No entries exist so add new Customer Entry to Visitors tep_db_query("insert into " . TABLE_VISITORS . " (date,customers_id,counter,browser_ip,browser_language,online,language,referer,uri) values (now(),'" . (int)$customer_id . "','1','".$b_ip."','".$b_lang."',now(),'".$language."','".tep_db_input($referer)."','".tep_db_input($uri)."')"); // A2. Add and update Customer entries in Visitors Trace if (((substr($b_lang, 0, 1) == '[') && $trace_spiders) || ((substr($b_lang, 0, 1) != '[') && $trace_users)){ tep_db_query("insert into " . TABLE_VISITORS_TRACE . " (date,customers_id,browser_ip,uri) values (now(),'" . (int)$customer_id . "','".$b_ip."','".tep_db_input($uri)."')"); tep_db_query("update " . TABLE_VISITORS_TRACE . " set customers_id = '" . (int)$customer_id . "' where browser_ip = '" . $b_ip . "' AND to_days(now()) - to_days(date) < 1"); } } else { // B1. Either one or two entries exist if (tep_db_num_rows(tep_db_query("select counter FROM " . TABLE_VISITORS . " where (browser_ip = '" . $b_ip . "' || customers_id = '" . (int)$customer_id . "') AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1")) == '1') { // B1a. Only one Customer entry so just update it in Visitors tep_db_query("update " . TABLE_VISITORS . " set counter = counter+1, customers_id = '" . (int)$customer_id . "', online = now(), browser_ip = '" . $b_ip . "' where (customers_id = '" . (int)$customer_id . "' || browser_ip = '" . $b_ip . "') AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1"); } else { // B1b. Must be two Customer entries so need to delete newest and update oldest in Visitors $counter_old_raw = tep_db_query("select browser_id, counter FROM " . TABLE_VISITORS . " where (browser_ip = '" . $b_ip . "' || customers_id = '" . (int)$customer_id . "') AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1 order by browser_id DESC limit 1"); $counter_old = tep_db_fetch_array($counter_old_raw); $counter_sum = $counter_old["counter"]; $browser_id = $counter_old["browser_id"]; tep_db_query("delete from " . TABLE_VISITORS . " where browser_id = '" . $browser_id . "'"); tep_db_query("update " . TABLE_VISITORS . " set counter = counter + '" . $counter_sum . "' + 1, customers_id = '" . (int)$customer_id . "', online = now(), browser_ip = '" . $b_ip . "' where (customers_id = '" . (int)$customer_id . "' || browser_ip = '" . $b_ip . "') AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1"); } // B2. Add and update Customer entries in Visitors Trace if (((substr($b_lang, 0, 1) == '[') && $trace_spiders) || ((substr($b_lang, 0, 1) != '[') && $trace_users)){ tep_db_query("insert into " . TABLE_VISITORS_TRACE . " (date,customers_id,browser_ip,uri) values (now(),'" . (int)$customer_id . "','".$b_ip."','".tep_db_input($uri)."')"); tep_db_query("update " . TABLE_VISITORS_TRACE . " set customers_id = '" . (int)$customer_id . "', browser_ip = '" . $b_ip . "' where (browser_ip = '" . $b_ip . "' || customers_id = '" . $customer_id . "') AND to_days(now()) - to_days(date) < 1"); } } } else { // 2. We dont know who customer is // Add Anonymous entry to Visitors if (!tep_db_num_rows(tep_db_query("select counter FROM " . TABLE_VISITORS . " where browser_ip = '" . $b_ip . "' AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1"))) { // A. New Anonymous entry to Visitors tep_db_query("insert into " . TABLE_VISITORS . " (date,counter,browser_ip,browser_language,online,language,referer,uri) values (now(),'1','".$b_ip."','".$b_lang."',now(),'".$language."','".tep_db_input($referer)."','".tep_db_input($uri)."')"); } else { // B. Update Anonymous entry to Visitors tep_db_query("update " . TABLE_VISITORS . " set counter = counter+1, online = now() where browser_ip = '" . $b_ip . "' AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1"); } // C. Add Anonymous entry to Visitors Trace if (((substr($b_lang, 0, 1) == '[') && $trace_spiders) || ((substr($b_lang, 0, 1) != '[') && $trace_users)){ tep_db_query("insert into " . TABLE_VISITORS_TRACE . " (date,customers_id,browser_ip,uri) values (now(),'" . (int)$customer_id . "','".$b_ip."','".tep_db_input($uri)."')"); } } } ?>