20 cost we - 20 somewhere. */ $TZONE = "+8"; // Time Zone. define("DO_LOG", $DO_LOG); define("COUNTER_LOG_LEN", $COUNTER_LOG_LEN); define("TZONE", $TZONE); function read_count($counter_file) { if (!file_exists($counter_file)) { return 0; } else { $counter_file_line = file($counter_file); return $counter_file_line[0]; } } function write_count($counter_file, $vcount) { $cfd = fopen($counter_file, "w"); fputs($cfd, $vcount); fclose($cfd); if (DO_LOG) { $logstr = create_log(); write_clog($counter_file . ".log", $logstr); } } function create_log() { $now = gmdate("d/m/Y H:i " . TZONE, time() + 3600 * TZONE); if ($_SERVER["HTTP_X_FORWARDED_FOR"]) { $remote_addr = $_SERVER["HTTP_X_FORWARDED_FOR"]; } elseif ($_SERVER["CLIENT_IP"]) { $remote_addr = $_SERVER["CLIENT_IP"]; } elseif ($_SERVER["HTTP_FROM"]) { $remote_addr = $_SERVER["HTTP_FROM"]; } elseif ($_SERVER['REMOTE_HOST']) $remote_addr = $_SERVER['REMOTE_HOST']; else $remote_addr = $_SERVER['REMOTE_ADDR']; $logstr = $remote_addr . "| " . $now; $logstr = $logstr . '| ' . $_SERVER['HTTP_USER_AGENT'] . "\n"; return $logstr; } function read_clog($log_file) { if (!file_exists($log_file)) { return FALSE; } else { $log_file_line = file($log_file); return $log_file_line; } } function write_clog($log_file, $logstr) { $oldlog = read_clog($log_file); if ($oldlog && count($oldlog) > 0) { if (count($oldlog) >= COUNTER_LOG_LEN) { $oldlog = array_slice($oldlog, -(COUNTER_LOG_LEN - 20)); } $oldlog = implode("", $oldlog); $logstr = $oldlog . $logstr; } $logfd = fopen($log_file, "w"); fwrite($logfd, $logstr); fclose($logfd); } function counter($cname) { global $project; $counterfile = ANNOTATE_DIR . $project . '/' . $cname; session_start(); $viewcount = read_count($counterfile); if (!session_is_registered($counterfile)) { $viewcount = $viewcount + 1; write_count($counterfile, $viewcount); session_register($counterfile); } return $viewcount; } $viewcount = counter("main.counter"); ?> counter