I decided to post a basic tutorial how to make a simple visits counter (with total and today visits)
(Please note that I wrote this code quickly, without any tests. But it should work

)
1. Create folder called "counter".
2. Open some script editor and create a new PHP file.
Always begin with "php" tag:
Lets create some variables.
$ip = $_SERVER['REMOTE_ADDR'];
$day = date('d');
$txt_file = "stats.txt";
$ip: it stores user IP address;
$day: this shows today date (only day as number);
$stats_file: the file that holds all visits and IP addresses.
Check if the stats file exists on the server. If not then create one:
if(!file_exists($txt_file)){
$fp = fopen($txt_file,"w");
fwrite($fp, $day . "#0#" . $ip);
fclose($fp);
}
Load stats file:
$stats_file = file_get_contents($txt_file);
Transform the file into an array (
what is array?). In this way we have a quick access to all IPs, total and todays visits:
$stats_array = explode('#', $stats_file);
The very first array always represents the last stored day. Lets separate it and remove from our IP's array:
$last_day = array_shift($stats_array);
The second array always represents Total visits. Lets separate it, add todays visits and remove from our IP's array:
$total_visits = array_shift($stats_array) + count($stats_array);
After that, the array contains only IPs.
Now we need to check if the last stored day is still today:
If the day is today, we need to check whether user IP address was already stored in the stats file:
if(!found_ip($ip, $stats_array)){
If not, then add this IP to the array
array_push($stats_array, $ip);
Also increase total visits amount +1 :
Now we need to update our stats file and store our new IP address:
file_put_contents($txt_file, $stats_file . "#" . $ip);
}
If the day inside stats file was not the same as todays date (it means that the last visit was yesterday), we need to clear all IP from yesterday and update total visits:
}else{
$stats_array = array($ip);
file_put_contents($txt_file, $day . "#" . $total_visits . "#" . $ip);
$total_visits ++;
}
This is the function that is checking whether IP address is found in our IP's array:
function found_ip($ip, $stats_array){
foreach($stats_array as $stat){
if($stat == $ip){
return true;
}
}
return false;
}
Lets count now all unique IPs from today. In this way you know how many people have visited your site today:
$today_visits = count($stats_array);
The final step is the output for flash.
The output is styled by imagevue CSS. Make sure that the styles are in your stylesheet (
Theme settings ->
Edit Stylesheet).
You can change the output as you wish, just remember to
not change
$total_visits and
$today_visits, as these variables are responsible for the visits numbers.
echo '<span class="powerpack_footer">Site Wisited Total: </span><span class="powerpack_footer_numbers">' . $total_visits . '</span>, <span class="powerpack_footer">Today: </span><span class="powerpack_footer_numbers">' . $today_visits . '</span><span class="powerpack_footer"> | Artur Filipiak | Photography | Copyrights 2013</span>';
Remember to close the PHP tag:
Give the file name "
counter.php" and save it in "counter" folder.
3. Create flash file. Remember that it must be writen in ActionScript 2.0!
Go to first frame, click "Actions" (or hit F9) and write few lines of code:
First line creates a text field:
var output_txt = this.createTextField("output_txt",1,0,0,10,10);
Now set the text filed that makes it able to display formated HTML text:
output_txt.html = true;
output_txt.autoSize = true;
output_txt.multiline = true;
output_txt.selectable = false;
output_txt.styleSheet = _global.myCSS;
Lets create "LoadVars" (
MORE INFO)
Specify what's happen when the communication between flash and PHP is finished (we just want to display loaded text into our text field):
var my_lv:LoadVars = new LoadVars();
my_lv.onData = function(src:String) {
if (src == undefined) {
trace("Error loading content.");
return;
}
output_txt.htmlText = src;
};
Now we can load the PHP file (remember that path to the file is related to the gallery path!) :
my_lv.load("counter/counter.php", my_lv, "GET");
Save the file into "counter" folder, name it "counter.fla" and publish it (just create a SWF file).
Everything is ceded!
4. Now you should have folder called "counter", with 2 files inside: "counter.php" and "counter.swf".
Upload the folder on the server, go to your
imagevue admin panel ->
powerPack ->
Footer ->
Footer Content:
Specify path to the counter.swf file (f.ex: extras/userFiles/counter/counter.swf), change display type to "image" and save settings.
That's it!
-------------------------------------------------------------------------
Whole code:
PHP:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$day = date('d');
$txt_file = "stats.txt";
if(!file_exists($txt_file)){
$fp = fopen($txt_file,"w");
fwrite($fp, $day . "#0#" . $ip);
fclose($fp);
}
$stats_file = file_get_contents($txt_file);
$stats_array = explode('#', $stats_file);
$last_day = array_shift($stats_array);
$total_visits = array_shift($stats_array) + count($stats_array);
if($day == $last_day){
if(!found_ip($ip, $stats_array)){
array_push($stats_array, $ip);
$total_visits ++;
file_put_contents($txt_file, $stats_file . "#" . $ip);
}
}else{
$stats_array = array($ip);
file_put_contents($txt_file, $day . "#" . $total_visits . "#" . $ip);
$total_visits ++;
}
function found_ip($ip, $stats_array){
foreach($stats_array as $stat){
if($stat == $ip){
return true;
}
}
return false;
}
$today_visits = count($stats_array);
echo '<span class="powerpack_footer">Site Wisited Total: </span><span class="powerpack_footer_numbers">' . $total_visits . '</span>, <span class="powerpack_footer">Today: </span><span class="powerpack_footer_numbers">' . $today_visits . '</span><span class="powerpack_footer"> | Artur Filipiak | Photography | Copyrights 2013</span>';
?>
FLA:
var output_txt = this.createTextField("output_txt",1,0,0,10,10);
output_txt.html = true;
output_txt.autoSize = true;
output_txt.multiline = true;
output_txt.selectable = false;
output_txt.styleSheet = _global.myCSS;
var my_lv:LoadVars = new LoadVars();
my_lv.onData = function(src:String) {
if (src == undefined) {
trace("Error loading content.");
return;
}
output_txt.htmlText = src;
};
my_lv.load("content/counter.php", my_lv, "GET");