schmitsz
schmitsz

Reputation: 175

PHP page visit logger

So I wanted to make something like a counter for visits for every page AND directory. The purpose was that of a security perspective but so far I've only managed to think of this:

<?php

include('connect.php');
$date = date("d.m.Y H:i:s",time());
$url = $_SERVER['REQUEST_URI'];
$ip = $_SERVER['REMOTE_ADDR'];

$query = "INSERT INTO `visits` VALUES ('','$date','$url','$ip')";

mysql_query($query);

?>

Problem is that in order for this to work I need to include this snippet in each file which isn't exactly what I need since, for example, I can't include it in any of my files in the /js/ folder. How can I make use of something like this and log each visit on each page individually?

Upvotes: 1

Views: 554

Answers (1)

ynnus
ynnus

Reputation: 241

If you have common scripts for every page you should start thinking about a landing page which is actualy delivering the content to the users request. This could be your index.php and your pages are accessed by an page-id (for example /index.php?show=home) or use a url-rewrite engine from the webserver to rewrite www.yourdomain.de/home to index.php?show=home so every page request starts at index.php and you can track the visitor and the requested page from the "show"-parameter. So you don't have lots of php files but only one for start and load the desired content from this starting point.

So first thing to do would be to reorganize your page to a single landing page with commonly executed scripts (like session handling, tracking, logging etc).

Upvotes: 1

Related Questions