Skip to content

Commit

Permalink
add visit log and counter
Browse files Browse the repository at this point in the history
  • Loading branch information
isecret committed Mar 15, 2023
1 parent ec75695 commit bc66bad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
11 changes: 11 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@
$data = $stmt->fetchAll();
if ($data) {
$url = current($data)['url'];
$url_id = current($data)['id'];
$ip = get_client_ip();
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$visit_count = current($data)['visit_count'] + 1;

$stmt = $db->prepare("update url set visit_count=? where id = ?");
$stmt->execute([$visit_count, $url_id]);

$stmt = $db->prepare("insert into visit_history(url_id, ip, user_agent) values (?, ?, ?)");
$stmt->execute([$url_id, $ip, $user_agent]);

header("Location: {$url}");
} else {
header("Location: /");
Expand Down
13 changes: 12 additions & 1 deletion mysql.db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ CREATE TABLE `url` (
`url` text NOT NULL,
`ip` varchar(32) NOT NULL,
`user_agent` text,
`visit_count` int(11) NOT NULL DEFAULT 0,
`create_date` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_code` (`code`),
KEY `idx_hash` (`hash`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;

CREATE TABLE `visit_history` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url_id` int(11) NOT NULL,
`ip` varchar(32) NOT NULL,
`user_agent` text,
`create_date` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_url_id` (`url_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;
Binary file modified sqlite.db.exp
Binary file not shown.

0 comments on commit bc66bad

Please sign in to comment.