|
|
|
shougan
Sid Upadhyay |
Posted:
Wed May 12, 2004 7:53 pm |
|
|
|
PRO GOLD
Joined: 26 Apr 2004
Posts: 5290
Location: Western Hemisphere
|
| Code: |
#!/usr/bin/perl -w
use strict; # always use this in Perl programs especially for the web
my $counter; # declare our sole variable
# this is where the count is stored
# open the count file with read/write access
open (COUNT, "+<count.txt") or die "Could not open count: $!";
# lock it, read it, then increment the count
flock(COUNT, 2);
$counter=<COUNT>;
$counter++;
# store the newly incremented count in the count file
seek(COUNT, 0, 0);
print(COUNT $counter);
truncate(COUNT, tell(COUNT));
close (COUNT);
# send the new count to the web page
print "Content-type: text/html\n\n";
print $counter; |
Think this will work on a shtml page with the counter.txt fille
This is a counter i got help in writing....
|
|
|
|
|
|
|
|
Back to top
|
|
|
|
shougan
Sid Upadhyay |
Posted:
Thu May 13, 2004 12:14 am |
|
|
|
PRO GOLD
Joined: 26 Apr 2004
Posts: 5290
Location: Western Hemisphere
|
Any thoughts.....
I got some help and came up with this...
| Code: |
<?php
class acounter {
var $config = array();
function acounter () {
$this->config['img'] = "http://nova.hbx.us/~shougan/digits/";
$this->config['animated_img'] = "http://nova.hbx.us/~shougan/digits_ani/";
$this->config['pad'] = 4;
$this->config['width'] = 16;
$this->config['height'] = 22;
$this->config['block_ip'] = false;
$this->config['logfile'] = "./ip.txt";
$this->config['block_time'] = 15;
}
function is_new_visitor() {
$is_new = true;
$rows = @file($this->config['logfile']);
$this_time = time();
$ip = getenv("REMOTE_ADDR");
$reload_dat = fopen($this->config['logfile'],"wb");
flock($reload_dat, 2);
for ($i=0; $i<sizeof($rows); $i++) {
list($time_stamp,$ip_addr) = split("\|",$rows[$i]);
if ($this_time < ($time_stamp+$this->config['block_time'])) {
if (chop($ip_addr) == $ip) {
$is_new = false;
} else {
fwrite($reload_dat,"$time_stamp|$ip_addr");
}
}
}
fwrite($reload_dat,"$this_time|$ip\n");
flock($reload_dat, 3);
fclose($reload_dat);
return $is_new;
}
function read_counter_file($page) {
$update = false;
if (!file_exists("./pages/$page.txt")) {
$count_dat = fopen("./pages/$page.txt","w+");
$this->counter = 1;
fwrite($count_dat,$this->counter);
fclose($count_dat);
} else {
$fp = fopen("./pages/$page.txt", "r+");
flock($fp, 2);
$this->counter = fgets($fp, 4096);
flock($fp, 3);
fclose($fp);
if ($this->config['block_ip']) {
if ($this->is_new_visitor()) {
$this->counter++;
$update = true;
}
} else {
$this->counter++;
$update = true;
}
if ($update) {
$fp = fopen("./pages/$page.txt", "r+");
flock($fp, 2);
rewind($fp);
fwrite($fp, $this->counter);
flock($fp, 3);
fclose($fp);
}
}
return $this->counter;
}
function create_output($page='') {
if (empty($page)) {
$page = "counter";
}
$this->read_counter_file($page);
$this->counter = sprintf("%0"."".$this->config['pad'].""."d",$this->counter);
$ani_digits = sprintf("%0"."".$this->config['pad'].""."d",$this->counter+1);
$html_output = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr align=\"center\">\n";
for ($i=0; $i<strlen($this->counter); $i++) {
if (substr("$this->counter",$i,1) == substr("$ani_digits",$i,1)) {
$digit_pos = substr("$this->counter",$i,1);
$html_output .= "<td><img src=\"".$this->config['img']."$digit_pos.gif\"";
} else {
$digit_pos = substr("$ani_digits",$i,1);
$html_output .= "<td><img src=\"".$this->config['animated_img']."$digit_pos.gif\"";
}
$html_output .= " width=\"".$this->config['width']."\" height=\"".$this->config['height']."\"></td>\n";
}
$html_output .= "</tr></table>\n";
return $html_output;
}
}
?>
|
Then the page with the counter has
| Code: |
<?php
include_once "acounter.php";
$ani_counter = new acounter();
echo $ani_counter->create_output("nova.hbx.us/~shougan/index.html");
?>
|
Which one should i use..The perl or the php???
|
|
|
|
|
|
|
|
Back to top
|
|
|
|
Weaver
|
Posted:
Thu May 13, 2004 12:35 am |
|
|
|
PROfessional Member
Joined: 18 Jun 2002
Posts: 2587
Location: /home/weaver/
|
|
If you are coming up with solutions like the PHP posted above, then I don't think we are ones to be telling you what you should be using. I personally would travel the PHP route, although the solution you have provided is more complex than I would like a file based solution to be. Personally, if you desire the functionality of the solution you have provided, a MySQL (as oppose to file) backend would be much more appropriate.
-Weaver
|
|
|
|
|
|
|
|
Back to top
|
|
|
|
shougan
Sid Upadhyay |
Posted:
Thu May 13, 2004 12:38 am |
|
|
|
PRO GOLD
Joined: 26 Apr 2004
Posts: 5290
Location: Western Hemisphere
|
|
Its just counters...lol. I just dont want to use services...
|
|
|
|
|
|
|
|
Back to top
|
|
|
|
|
Back to top |
|
|
|
|
|