Page 1 of 1

Visualize visits on a graph

Posted: Sun Mar 18, 2012 1:40 am
by migul
Hi,
I downloaded the visit counter for my personal site and added some functionalities that you might be interested in including in the original script -or not-. I added a history so the counts at the end of a day will be stored in a file together with the date so you can, for example, plot the number of visits as a function of time.

The code I wrote to create the history is

Code: Select all

/*  Print to visits history */
   $datetoday = date("Y m d");
   $hislogfile = "logs/" . "history_" . $page . ".txt";

   $vh = @fopen($hislogfile,"a+");
   $lastdateinfile = exec("tail -1 $hislogfile | cut -c1-10");
   $lastdateinfile2 = trim ($lastdateinfile);
   if ( $datetoday == $lastdateinfile2 ) {
     $numberoflines = exec("cat $hislogfile | wc -l");
     exec("sed '$numberoflines d' < $hislogfile > logs/temp; cp logs/temp $hislogfile");
     fputs($vh, $datetoday."    ".$count."\n");
     fclose($vh);
     }
   else {
     fputs($vh, $datetoday."    ".$count."\n");
     fclose($vh);
Also, I created a php script that invokes gnuplot to create a graph that is generated in real time with the history data - of course gnuplot needs to be installed in the server.

You can check the "visitors" link at the bottom of my page http://miguelcaro.com.es/ if you are interested.

Just thought some people might be interested in this kind of functionality (I am) without needing to rely on some external provider as Google or so.

Miguel

Re: Visualize visits on a graph

Posted: Sun Mar 18, 2012 9:13 am
by Klemen
Thanks for sharing the mod!

Re: Visualize visits on a graph

Posted: Sun Mar 18, 2012 9:31 am
by Henrie
Hello Migul,

It would be great if you could also share the php-script you made to create the graph that is generated in real time with the history data.

Greetings,
Henrie

Re: Visualize visits on a graph

Posted: Sun Mar 18, 2012 10:42 am
by migul
Of course! The following is the script to call gnuplot, that needs to be installed on the server. Of course, www-data or whatever your Apache/web server user is has to have write access to the output file:

Code: Select all

<?php

/* Remember the webserver running PHP must have write */
/* access to the gnuplot output file or the directory */
/* where it is going to be created */
exec("gnuplot gnuplot.script");

echo "<html style=\"text-align:justify;width:640;
margin-left:auto;margin-right:auto\">
";

echo "<head>
<meta name=\"Content-Script-Type\" content=\"text/javascript\">
</head>
";

echo "<h1>Evolution of the number of visitors with time</h1>
";

$visitsfile = @fopen("../logs/mainpage.txt", "r");
$numvisits = fgets($visitsfile,1024);

echo "<div>The total number of visitors so far is <b>".$numvisits."</b>.
A \"visitor\" is a hit on the main page <b>miguelcaro.com.es</b>
from any given computer separated by at least one hour
from the previous hit from that computer.</div>
";

echo "<p id=\"graphContainer\"><img id=\"graph\" width=\"640\" height=\"280\"
alt=\"Visits graph should go here... something might be wrong with
your browser.\"
src=\"visits.png?".time()."\" /></p>
";

echo "<script type=\"text/javascript\">
<!--
image = document.getElementById('graph');
container = image.parentNode;
text = document.createTextNode('Please, wait while the graph is generated...');
error = document.createTextNode('Sorry, an error occurred while trying to generate the graph');
container.replaceChild(text, image);
cache = new Image;
cache.onerror = function () {container.replaceChild (error, text)};
cache.onload = function () {container.replaceChild (image, text)};
cache.src = image.src;
// -->
</script>
";

echo "</html>
";

?>
And this is the gnuplot script that I use to plot the history file generated with the code of my first post:

Code: Select all

set term png size 640,280 enhanced font "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf" 12
set output "visits.png"
#
set xdata time
set timefmt "%Y %m %d"
set format x "%d/%m/%y"
set xtics rotate
#
set grid
#
set title "Number of visitors to {//usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf miguelcaro.com.es} since 12/03/2012"
#
plot "../logs/history_mainpage.txt" u 1:4 w lp lc 1 lw 2 pt 7 ps 1.2 notitle

Re: Visualize visits on a graph

Posted: Sun Mar 18, 2012 10:51 am
by migul
I tell gnuplot where the font is explicitly as my Debian server has trouble with the format "font Times 12", and the default font does not scale. You would have to find a suitable font file in your server. I think in a Ubuntu one it should be much easier, and you can use Arial, or Times, without specifying the location of the file.

Re: Visualize visits on a graph

Posted: Sun Mar 18, 2012 3:38 pm
by Henrie
Thank you for sharing.
Now I have to find some time to implement it on my webpage where i already use the counter.