To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-14-2003, 12:35 PM   #1
bad76
GuruMeditation #8401.0007
 
bad76's Avatar
 
Join Date: May 2003
Location: Italy
Posts: 1,213
Just a counter...

Hi,
i'm sorry, but i can't post any other session handler script...
This is a thing of "newbies"... a counter.

My quest is to do this without overload the db, but storing periodically the access, and i can't use any scheduler (i'm not the host-master...).

Well, a little comment.
This script is in the file i want to count access:

<script><!--
new Image().src = "count.php?name=<name_of_file>";
//--></script>

My site is also an intranet, so more access going from our gateway, so i need also to check for the port....

I will use a file for each counter.

PHP Code:
<?php

//check if is the home page access...
if(substr_count($nome,"main")>0) $main=1; else $main=0;

//the real name is name+path
$name=$PATH_TO_DIR.$name;
//clear vars...
$line[0]=0; $line[1]=0; $line[2]=0;

//get IP and PORT of user
$myip=$REMOTE_ADDR;
$curip=$myip.$REMOTE_PORT;

//mask last two number of port:
// Any user can open many browser window...
$curip=substr($curip,0,strlen($curip)-2);

//read file and delete blank.
$line=@file($name);
$line[0]=trim($line[0]);   // counter
$line[1]=trim($line[1]);   // IP.PORT
$line[2]=trim($line[2]);   // day - only for home page counter...

//check if is me or if is previous user
if($myip!="192.168.xxx.xxx" && $curip!=$line[1]){
    
$line[0]++;  //inc access counter
    
$line[1]=$curip;  

    if (
$main==0) { // if is not the main write it into file.
        
$handle=fopen($name, "w+");
        
fwrite($handle,$line[0]."\n".$line[1]);
    } else {        
        if(
$line[2]!=date("d")) {
            
//if is tomorrow write all counters
            //into db.
            
require("storedb.php");
        }
        
//update the date and write the file
        
$line[2]=date("d");  
        
$handle=fopen($name, "w+");
        
fwrite($handle,$line[0]."\n".$line[1]."\n".$line[2]);
    }  
//exit safety
    
fclose($handle);
}

header('Content-type: image/gif');
readfile("../gfx/null.gif");
?>
That's all... What you think?
__________________
It's statistically proven that anything can be proven by means of statistics...
bad76 is offline   Reply With Quote
Old 11-03-2003, 09:16 PM   #2
LegolasArcher
Junior Member
 
Join Date: Nov 2003
Location: New Jersey
Posts: 1
Why don't you output to a text file?
LegolasArcher is offline   Reply With Quote
Old 11-03-2003, 11:16 PM   #3
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
he doesn't? what's this then?
PHP Code:
        $handle=fopen($name, "w+");
        
fwrite($handle,$line[0]."\n".$line[1]."\n".$line[2]);
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 11-04-2003, 02:30 AM   #4
bad76
GuruMeditation #8401.0007
 
bad76's Avatar
 
Join Date: May 2003
Location: Italy
Posts: 1,213
I think he say: "why to use a db? put more info in file!"

After all I need to do some graph, with a lot of parameters. Without any db, it's a bit too complex.

And to complete store of info in file I've to do a more complex access to it, with field writing, sorting function, ...
And don't forgive: this is executed EVERY time a user go on a page. So this must be faster possible.
__________________
It's statistically proven that anything can be proven by means of statistics...
bad76 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 12:37 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.