Cool Compiler
Cool Compiler

Reputation: 857

page counter on different pages of website

<?php
ob_start();
session_start();
$counter_name = "index.txt";
// Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
  $f = fopen($counter_name, "w");
  fwrite($f,"0");
  fclose($f);
}
// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
// Has visitor been counted in this session?
// If not, increase counter value by one
if(!isset($_SESSION['hasVisited'])){
  $_SESSION['hasVisited']="yes";
  $counterVal++`enter code here`;
  $f = fopen($counter_name, "w");
  fwrite($f, $counterVal);
  fclose($f); 
}
?>

I am using this code to count the number of visitors on one page using sessions. But I want to keep page counter on all other pages as well. So when I copy and paste the code into other pages the value does not get incremented. It only gets incremented on first page but not on other page I visit. What is the possible solution?

Upvotes: 0

Views: 600

Answers (2)

JamesSkinner12
JamesSkinner12

Reputation: 23

I do this on my website by storing information in a database. It's more secure than using a txt file, so you should consider doing that. And you can use the IP along with SESSION to be more thorough with record keeping. Make a traffic logging file to include in your header file that will use the request URI, IP, session id/user id.

By using request URI you can put the one single traffic logging script on all pages to handle all the different web pages you want to track. I can add some code examples if you want.

Hope this helps you out!

*/ 
Define Variables You Want To Store..In this case it is preparing a timestamp, the page (i.e. the $_SERVER['REQUEST_URI']), and the IP
/*

$time = time();
$page = $_SERVER['REQUEST_URI'];

//This is used to sort out the correct IP Address to store //

$client  = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP)) {
        $ip = $client;
    } elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
        $ip = $forward;
    } else {
        $ip = $remote;
    }

//Use this if you want to see if the data exists already, I usually just store all traffic to make sure people aren't up to anything fishy//



$stmt = $database->prepare("SELECT ID FROM traffic WHERE IP = :IP AND page = :page");
$stmt->bindParam(':IP', $ip);
$stmt->bindParam(':page', $page);
$stmt->execute();
$result = $stmt->FetchALL(PDO::FETCH_ASSOC);

if (!isset($result)) {
     $sql = "INSERT INTO traffic (time,IP,page) VALUES (:time, :ip, :page)";
     $params = array(
             ':time' => $time,
             ':ip' => $ip,
             ':page' => $page,
         );
     $stmt = $database->prepare($sql);
     $stmt->execute($params);
}

Upvotes: 1

jeam fox
jeam fox

Reputation: 1

You need use in mysql or sqlite or other database server for save a name and number of visits. You need first of all create database and learn how use it. Don't run.... With TXT files you can't arrive far...

Upvotes: -2

Related Questions