General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
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:
  #1  
Old July 15th, 2002, 03: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!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek