A Digital Age Deserves A Digital Leader

Will this work...

Will this work...

Postby shougan » Wed May 12, 2004 11:53 pm

Code: Select all
#!/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....
Do you want the latest news on Betas & Software, Security, or even Vista? Check out our <a href="http://www.pro-networks.org/forum/home_index.php" target="_blank" class="postlink">news areas</a> and be sure to <a href="http://www.pro-networks.org/main/newsletters.htm" target="_blank" class="postlink">subscribe to our PROneT newsletters!</a>
PRO Level 15
Posts: 1174
Joined: Tue Apr 27, 2004 2:12 am
Location: Western Hemisphere

Postby shougan » Thu May 13, 2004 4:14 am

Any thoughts.....

I got some help and came up with this...
Code: Select all
<?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: Select all
<?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???
PRO Level 15
Posts: 1174
Joined: Tue Apr 27, 2004 2:12 am
Location: Western Hemisphere

Postby Weaver » Thu May 13, 2004 4:35 am

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
Public Keys

The primary purpose of the DATA statement is to give names to constants; instead of referring to pi as 3.141592653589793 at every appearance, the variable PI can be given that value with a DATA statement and used instead of the longer form of the constant. This also simplifies modifying the program, should the value of pi change.
-- FORTRAN manual for Xerox Computers
PROfessional Member
User avatar
Posts: 1967
Joined: Wed Jun 19, 2002 12:05 am
Location: /home/weaver/

Postby shougan » Thu May 13, 2004 4:38 am

Its just counters...lol. I just dont want to use services...
PRO Level 15
Posts: 1174
Joined: Tue Apr 27, 2004 2:12 am
Location: Western Hemisphere

Return to HTML, CSS, and Scripts

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron
cron