· 7 years ago · Oct 08, 2018, 04:30 AM
1<?php
2// Disclaimer: junktastic code, don't use as learning material!
3// A simple counter script for php.
4?>
5<!DOCTYPE html>
6<html>
7 <head>
8 <title>OMG COUNTERS!</title>
9 </head>
10 <body>
11<?php
12
13function init_database($db) {
14 // Should use CREATE TABLE IF NOT EXISTS, but I'm linked against sqlite 2.8 instead of 3+
15 sqlite_query($db, 'CREATE TABLE Counters (page TEXT PRIMARY KEY, count INTEGER DEFAULT 0)');
16 sqlite_query($db, "INSERT OR IGNORE INTO Counters (page, count) VALUES ('index', 0)");
17}
18
19if ($db = sqlite_open('counter.db', 0666, $sqliteError)) {
20 if (array_key_exists('initdb', $_GET) && $_GET['initdb'] === 'true') {
21 init_database($db);
22 }
23 sqlite_query($db, "UPDATE Counters SET count = count + 1 WHERE page = 'index'");
24 $count = sqlite_fetch_single(sqlite_query($db, "SELECT count FROM Counters WHERE page = 'index'"));
25 sqlite_close($db);
26
27 echo "<p>$count page visits!</p>";
28} else {
29 echo "<p>OMGWTFERROR: $sqliteError</p>";
30}
31
32?>
33 </body>
34</html>