Click to See Complete Forum and Search --> : Just a counter...


bad76
10-14-2003, 12:35 PM
Hi,
i'm sorry, but i can't post any other session handler script... :)
This is a thing of "newbies"... a counter.

My quest is to do this without overload the db, but storing periodically the access, and i can't use any scheduler (i'm not the host-master...).

Well, a little comment.
This script is in the file i want to count access:

<script><!--
new Image().src = "count.php?name=<name_of_file>";
//--></script>

My site is also an intranet, so more access going from our gateway, so i need also to check for the port....

I will use a file for each counter.


<?php

//check if is the home page access...
if(substr_count($nome,"main")>0) $main=1; else $main=0;

//the real name is name+path
$name=$PATH_TO_DIR.$name;
//clear vars...
$line[0]=0; $line[1]=0; $line[2]=0;

//get IP and PORT of user
$myip=$REMOTE_ADDR;
$curip=$myip.$REMOTE_PORT;

//mask last two number of port:
// Any user can open many browser window...
$curip=substr($curip,0,strlen($curip)-2);

//read file and delete blank.
$line=@file($name);
$line[0]=trim($line[0]); // counter
$line[1]=trim($line[1]); // IP.PORT
$line[2]=trim($line[2]); // day - only for home page counter...

//check if is me or if is previous user
if($myip!="192.168.xxx.xxx" && $curip!=$line[1]){
$line[0]++; //inc access counter
$line[1]=$curip;

if ($main==0) { // if is not the main write it into file.
$handle=fopen($name, "w+");
fwrite($handle,$line[0]."\n".$line[1]);
} else {
if($line[2]!=date("d")) {
//if is tomorrow write all counters
//into db.
require("storedb.php");
}
//update the date and write the file
$line[2]=date("d");
$handle=fopen($name, "w+");
fwrite($handle,$line[0]."\n".$line[1]."\n".$line[2]);
} //exit safety
fclose($handle);
}

header('Content-type: image/gif');
readfile("../gfx/null.gif");
?>


That's all... What you think?

LegolasArcher
11-03-2003, 09:16 PM
Why don't you output to a text file?

Moonglobe
11-03-2003, 11:16 PM
he doesn't? what's this then?
$handle=fopen($name, "w+");
fwrite($handle,$line[0]."\n".$line[1]."\n".$line[2]);

:)

bad76
11-04-2003, 02:30 AM
I think he say: "why to use a db? put more info in file!"

After all I need to do some graph, with a lot of parameters. Without any db, it's a bit too complex.

And to complete store of info in file I've to do a more complex access to it, with field writing, sorting function, ...
And don't forgive: this is executed EVERY time a user go on a page. So this must be faster possible.