Class Views {
Private $update _time = 1800; Update, Unit seconds
Private $log _dir = './www.111cn.net/log/'; File storage Directory
Private $pid = ' 0 '; Article ID
function __construct ($po _id) {
$pid = (is_numeric ($po _id) && $po _id > 0)? Intval ($po _id): 0; Get article ID
$this->pid = $pid;
}
Read the document and click the number + 1
function Read () {
if ($this->pid > 0) {
$s = "";
$filename = Site_root $this->log_dir. ' Click_log.txt '; Filename
if (file_exists ($filename)) {
$content = file_get_contents ($filename);
if ($content) {
$dary = Explode ("", $content); Array the contents of the document
foreach ($dary as $line) {
$data _ary = explode (' | ', $line);
$ary [$data _ary[0]] = $data _ary[1];
}
}
if (array_key_exists ($this->pid, $ary)) {//check key exists in the array
$ary [$this->pid] = $ary [$this->pid] + 1; Add the number of clicks to the current article by 1
} else {
$ary [$this->pid] = 1; Article ID appears for the first time so give an initial value of 1
}
foreach ($ary as $key => $val) {
$s. = $key. ' | '. $val. " "; Array complete and revert to document format
}
Print_r ($s);
} else {
$s. = $this->pid. ' | 1 '. ' "; This is the initialization document Www.111cn.net
}
}
$fp = fopen ($filename, "w");
if (Flock ($FP, LOCK_EX)) {//Exclusive lock
Fwrite ($fp, $s);
Flock ($FP, lock_un); Release lock
}
Fclose ($FP);
@chmod ($filename, 0777);
}
}
$a = new Views ("3");
$a->read ();