-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhitcounter.php
More file actions
48 lines (41 loc) · 1.48 KB
/
hitcounter.php
File metadata and controls
48 lines (41 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
class HitCounter {
private $expire; //menentukan umur cookie
private $file = 'visitor.txt';
public function __construct() {
if (!file_exists($this->file)) {
//kondisi jika file visitor.txt belum ada, buat baru dengan nilai 0
$handle = fopen($this->file, 'w');
$data = 0;
fwrite($handle, $data);
}
$this->expire = 30 * 86400; //umur cookie 30 hari
}
public function Hitung() {
if (!isset($_COOKIE['counter'])) {
//cookie kosong dan tambahkan jumlah pengunjung
$handle = fopen($this->file, 'r');
$data = intval(fread($handle, filesize($this->file))); //mengambil nilai dari visitor.txt
$nilaibaru = $data + 1; //tambahkan nilai +1
//simpan nilai baru
$handle = fopen($this->file, 'w');
fwrite($handle, $nilaibaru);
setcookie('counter', time(), time() + $this->expire); //tambahkan cookie dengan nilai tanggal sekarang
}
}
public function tampil() {
//mengambil nilai dari visitor.txt
$handle = fopen($this->file, 'r');
$data = intval(fread($handle, filesize($this->file)));
return $data;
}
public function waktu() {
$history = null;
//menampilkan kapan user berkunjung
if (!empty($_COOKIE['counter'])) {
$get = $_COOKIE['counter'];
$history = date("d F Y", $get);
}
return $history;
}
}