General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Iron Speed
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old July 15th, 2002, 02:49 PM
retr0 retr0 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: UK
Posts: 15 retr0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Red face Need help improving daily visitors counter script

Hi Guys

I wrote this simple daily visitors counter script yesterday for a tutorial that I'm writing for my site demonstrating file handling in PHP. But am currently to embarrassed by my messy inefficient code it to post it. I could really do with a hand in improving it. I am sure that there are much more efficient ways to do what I've done.

Any ideas as to how I can make it better???

Retr0


Call the script and counter.txt and script file via includes.

Code:
<?php include("counter.txt"); ?>
<IMG border=0 width=1 height=1 src="daily_visitors/daily_visitors.php" >




<?php

//   Daily visitors counter script


    // Holds the current day of year as a numerical value 1 - 365
    $day = $PATH_INFO."day.txt";

    // Are counter value txt file
    $counter = $PATH_INFO."counter.txt";

    // Are IP address value txt file
    $ip = $PATH_INFO."ip.txt";

    // Number of previous IP address values to keep in txt file
    $ip_block = 200;


    $fp = fopen("$ip", "a");
    if (IsSet($REMOTE_ADDR))
    fwrite($fp, "$REMOTE_ADDR\n");

    else
    {
        $REMOTE_ADDR = 0;

    } // end else
    fclose($fp);


    $fp = fopen($ip, "r");
    $fr = fread($fp, filesize($ip));

    // explode $REMOTE_ADDR and place in a array
    $line = explode("$REMOTE_ADDR", $fr);

    // $i == $REMOTE_ADDR +1 for are \n
    for($i = 0; $i < count($line); $i++);
    fclose($fp);


    // Line numbers
    $fcontents = file ("$ip");
    while (list ($line_num, $line) = each ($fcontents))
    {

        if ($line_num > $ip_block)
        {
            $fp = fopen("$ip", "w");
            fwrite($fp, "");
            fclose($fp);
        } // end if

    } // end while



    if ($i > 2)
    {
        // Are IP address has been here recently
        exit;
    }


    else
    {

        // Open are day of year file
        $fd = fopen("$day", "r");
        $old = fgets($fd, 1024);
        fclose($fd);

        // If the day of year file is empty
        // write day of year as a numerical value 1 - 365
        If ($old == "")
        {
            $r = date("z");
            $fs = fopen($day, "w");
            fwrite($fs, "$r");
            fclose ($fs);
        } // end if

        // Increment 'z' day of year number
        If ($old == date("z"))
        {
            $o = 1 + date("z");
            $fs = fopen($day, "w");
            fwrite($fs, $o);
            fclose ($fs);

            // Reset are counter
            $fr = fopen("$counter", "w");
            fwrite($fr, "1");
            fclose($fr);
        } // end if

        else
        {
            // Read Counter
            $fp = fopen("$counter", "r");
            $count = fgets($fp, 1024);
            fclose($fp);

            // Current counter file empty so writing a 1
            If ($count == "")
            {
                $fr = fopen("$counter", "w");
                fwrite($fr, "1");
                fclose($fr);
            } // end if

            // Increment counter number
            $fw = fopen("$counter", "w");
            $output = $count++;
            $countnew = fputs($fw, $count);
            fclose($fw);
        } // end else


    } // end else



?>

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Need help improving daily visitors counter script


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway