This coding is an add-on which builds an image 'ctr2.jpg' for the counter of visits (CTR) if your are using in relation,
the statistic prog phpMyVisites
You have to implement a sub-directory /images, where would be stored the digit images of 0 to 9 naming them 1.jpg 2.jpg
up to 9.jpg.
The previous number of visits is saved in a text file named : compteur.txt
here is the code to implement in a file named ctr.php for example :
<?php
$host="localhost"; //indicate here the host for example localhost or the IP of the server
$user="root"; //indicate here the MySQL user's name for example root, though to avoid: rather create a user for php
$base="mybase"; //indicate here the name of the Data Base
$passe="password"; //indicate here the password
if (!($dblink=mySql_connect($host,$user,$passe)))
{
//echo "<h2>the Connection failed !</h2>\n";
// die ("<br><h2>Shut Down!</h2>\n");
exit();
}
$namtbl="phpmv_visit"; // name of the table to scan
$namfield="idvisit"; // name of the field in that table
$select_base=mySql_selectdb($base,$dblink); //select Data Base
$query="SELECT * FROM ".$namtbl; //Save in $query the request SQL
$dbresult=mysql_query($query,$dblink); //Execute the request
if (!(mysql_query($query,$dblink)))
{
//echo "<b>Sorry, the table ".$namtbl." is missing, </b>\n";
exit();
}
// find out the last record (idvisit)
$query="SELECT MAX(idvisit) FROM ".$namtbl; //Save the request of max(idvisit)
$result=mysql_query($query,$dblink); //Execute the request
if (!$result) {
// echo 'enable to execute the request : ' . mysql_error();
exit();
}
$row = mysql_fetch_row($result);
//echo $row[0]; // idvisit is the first field in that table
$visit = $row[0];
//echo "visit: ".$visit."</br>";
//Update Visits and Create an Image for the counter
$text="";
$fic="";
$Ndig=6; // number of digits to display
$bias=1234; // original bias if the CTR does not start from zero
//
$fp=fopen('compteur.txt','r'); //Open file compteur.txt for reading
$oldvisit=fgets($fp,20); // take 20 first bytes
fclose($fp); // close file
$dvisit = $visit - $oldvisit;
//echo " DVisites : ".$dvisit."</br>";
if ( $dvisit > 0) // test if one or more visits
{
$fp=fopen('compteur.txt','w'); // Open file for writing
fputs($fp,$visit); // save new value
fclose($fp); // close the file
//
$myvisit = $bias + $visit;
$text = strval($myvisit);
//echo " text : ".$text."</br>";
// the text to draw
$text=str_pad($text, $Ndig, "0", STR_PAD_LEFT);
// Create the image
$Dim = imagecreate(10*$Ndig, 12); // an image of (10xNdig x 12) pixels
$nx=0;
while($nx<strlen($text))
{
$digit=$text{$nx};
$fic="images/".$digit.".jpg"; // build the image of the CTR
$image = imagecreatefromjpeg($fic);
imagecopy($Dim, $image, $nx*10, 0, 0, 0, 10, 12);
$nx++;
}
$fh = fopen('ctr2.jpg','w'); // Open file for writing
imagejpeg($Dim,'ctr2.jpg');//save CTR image into file 'ctr2.jpg'
imagedestroy($Dim); // destroy the memory image of the CTR
}
?>
in order to get the CTR, include the following lines in your web page:
<!-- Update the visits CTR-->
<?php
include ("ctr.php");
?>
then display the CTR with <img src="ctr2.jpg" alt="ctr">