Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Create a connection to the database
- $database = new mysqli('localhost', 'username', 'password', 'databasename') or die('Could not connect to database');
- // Ensure that the user input is available and safe
- if (!isset($_REQUEST['file'])) die('no file id provided');
- $file = $_REQUEST['file'];
- if (preg_match("/[^0-9]/", $file)) die('bad file');
- $ip = $_SERVER['REMOTE_ADDR'];
- if (preg_match("/[^a-fA-F0-9\.:]/", $ip)) die('bad ip');
- // Add this visitor to the list
- $database->query("insert into ipcounter values(0, '$file', '$ip', now())") or init();
- // Do a mysql "search" to get the hitcounter
- $result = $database->query("select count(distinct ip) hits from ipcounter where file = '$file'") or die('error selecting hit count');
- // Read the result
- if ($result = $result->fetch_assoc())
- {
- // Show the hitcount
- echo $result['hits'];
- }
- else init();
- // This will create the required table if something goes fuck
- function init()
- {
- global $database;
- echo 'Re-initializing database due to error: ' . $database->error;
- $database->query('drop table if exists ipcounter') or die('could not delete table: ' . $database->error);
- $q = <<<Q
- create table if not exists ipcounter (
- id int(11) not null auto_increment,
- file int(11) not null,
- ip varchar(45) not null,
- time timestamp not null default current_timestamp,
- primary key (id),
- key (ip))
- Q;
- $database->query($q) or die('could not create table: ' . $database->error);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement