Solved

easy point for all experts

Posted on 1998-12-04
21
210 Views
Last Modified: 2011-09-20
Now, I am designing the home page for MCS department.
I have a very simple text counter written by C language.
After I compiled C program , I got 'a.out' and I include
this line in my welcome page <!--#exec cmd="../a.out"-->
and it is working, every time I have visited the home page,
the counter have increase by 1.

But my problems are : when I hit 'RELOAD' or 'Home'(home is
a link created by my self. The counter is still increase by
1 and I would like it remain constant. And it will increase by 1 when I visit it another day. So I would like the experts to teach my how to write perl for this problem.

The perl should be able to check :
If the REMOTE_HOST still same then will not call 'a.out'
if the REMOTE_HOST is different then call 'a.out'

I will be appreciated . Thanl you !
0
Comment
Question by:mcs436_2
  • 7
  • 5
  • 4
  • +5
21 Comments
 
LVL 28

Expert Comment

by:sybe
ID: 1206726
You have 2 options:

1. solve it server side (as you propose yourself). Then you have to store the recent IP-numbers somewhere (a file, a database) and read that everytime a visitor arrives. It seems easier to implement that in the C-code you have made to do the counting.

2. solve it client side. Give every browser that visits your site a cookie with an expiration of one day. Count only when you set a cookie.
0
 

Author Comment

by:mcs436_2
ID: 1206727
ok I think option 2 is not good because visitor can turn
cookies off.

I like option 1 but still have problem.
Let say I used perl to store IP in the log.txt.
and visitor # 1 visit it for first time. It will increase by 1.
But if today until next month, nobody visit this page and in the
log.txt still have first visitor's IP. Then maybe that visitor
visit it again for second time, because the IP will be same (
used same computer to visit it) and counter will not increase by
1 and it should be increase by 1.

What do you think ?
0
 
LVL 4

Expert Comment

by:martinag
ID: 1206728
Have to agree with sybe. It's *much* easier to implement this in your program directly than using Perl as some sort of bridge. It just messes things up.

Here's how I'd do:

Save ip and time (in hours since 1/1/70 for example) in a database. It seems much better to me with a database than a file since a file would become rather large if your site is popular and it'll take time reading/writing etc.

Get the right ip out of the database and compare the time with the current. If there's more than 24 hours diff, increase the count. Otherwise just leave it. Output count and update the date of the ip in the database.

Of course, you'll have to clean the file/database sometimes so it don't get too big. Just a few lines of code.

Martin
0
 

Expert Comment

by:philmike
ID: 1206729
1. You can set a cookie. (I don't know if this with work since it's in a page).

2.
if (-e "log.dat") {
 open(LOG,"<log.dat");
 $lastaddress = <LOG>;
 close(LOG);
}
if ($ENV{'REMOTE_ADDR'} ne $lastaddress) {
 $pass = "GOOD";
 open(LOG,">log.dat");
 print LOG "$ENV{'REMOTE_ADDR'}\n";
 close(LOG);
}

Put this add to top of your program
where is increment the count put
if ($pass eq "GOOD") {
 #increment counter
}

Phillip Michael
phil@obis.com
0
 
LVL 4

Expert Comment

by:martinag
ID: 1206730
The problem is that this will only check the last client. If you've got many visitors, there might still be an increment.
Also, $pass will alvays be "GOOD" since output contains \n and the input is not chomped. Either don't print \n or chomp the input (or do both to be save)...

In order to get this safe, you'll have to flock a dummy file (I guess there is something like that in C in case you want to use that instead of Perl).

philmike, As mcs436_2 said, he don't wan't to use cookies (I take it you read the question history before posting).

Martin
0
 

Author Comment

by:mcs436_2
ID: 1206731
sorry ! I have to reject your answer because I said already, I don't want a cookies

Thank you for your effort
0
 

Author Comment

by:mcs436_2
ID: 1206732
for  martinag  :

   OK ! let said I accept you solving method, includes time and ip. But I
think still cause a problem ( if I understand your method ) :

 Let assume  A visited my page at 10:00 am , counter should be increase by 1.
 and he hit the 'home' link ( home just bring A to welcome page.) or 'RELOAD',
 counter should not increase by 1 and he leave and turn netscape off at 10.30 am.

 Then after an hour, he open the netscape and go to my page, counter should be
 increase by 1. Although, he has visited this page an hour ago, but this time A is
 consider a new visitor.

 Can you how me to solve this problem ? Please
0
 
LVL 4

Expert Comment

by:martinag
ID: 1206733
Use cookies.

Martin
0
 
LVL 4

Expert Comment

by:martinag
ID: 1206734
I know you don't want it, but...about 90% of your visitors accepts them I guess. If they don't accept, don't care about them.

Now, I don't think SSI can set cookies, BUT you could print a JavaScript with the counter. That script will set the cookie and the CGI will read it (if it exists, it means that the user has been there before).

I'm no C programmer, but maybe you or sybe know enough to make this. Else post a request in the CGI area.

Martin
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206735
Does 'care about' mean 'count'?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 4

Expert Comment

by:martinag
ID: 1206736
Not necessarily, but it could. That's mcs436_2's decision (was it necessary to select such a user name? :-)

Martin
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206737
I thought mcs436_2's decision was to count visitors who don't accept cookies only on the first visit per day?
This might be done similarly to philmike's suggestion, but accumulating a days worth of addresses, and deleting the file at the end of the day.
(However, it doesn't deal with different visitors from the same address, or visitors who use more than one address)
0
 
LVL 2

Accepted Solution

by:
tpryor earned 200 total points
ID: 1206738
hello mcs436_2,

Well you picked one of the trickyest problems that we are all wrestling with trying to determine how many hits you have received.

What I do is record all hits to all of my pages as a matter of course in a log file then I write a filter for the log.  Now when i run my reports i count the ip only once per page per day. I also filter out all ip's i dont want to count like mine. This is fine for off-line reporting.

That would seem to be the best solution the only problem you will face then is performance because those log files will grow fast.  So if i wanted to do this real time reporting i would keep a count file that held the total hit count and a daily hit tracking file that records the ip the time and date.  (you could keep the hit count in the same file) Run your filter on the smaller dialy file (count ip's only once, set your time threshold) and update that way (the hit count). at a certain time once a day your main reporting filter
updates the daily file with a fresh hit count and all the unique ip's within the last hour (or whatever your time threshold is).

Now you can print out all your html first (with only an off by one hit count) and do the processing in the background. Now when you hit reload it will increment by one but the next time you hit reload it wont. (if no other newcomers have shown up by then).

Well i hope that helps,

Tom

BTW

I would write it all in perl, or just have the backend reporting and filtering in c
0
 

Author Comment

by:mcs436_2
ID: 1206739
I give up. I think I would like a perl counter script. and you should compare the ip and hour
so set the hours for 3. If ip A vistied at 1.00 pm, then check the log file, if previous ip is same as this ip and previous (hour - now) is less than or equal 3, counter should not be incremented. If the hour is greater than or equal to 3, then counter should be incremented.
increase by 1.

If ip is different then previous ip, then counter should be incremented.


So, Please any expert help me !
Thank you !
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206740
So, you've decided you want to check only the previous IP address, and not all IP addresses in the past 3 hours?
0
 

Author Comment

by:mcs436_2
ID: 1206741
Yes only check previous ip and hours and compare it with new ip and new hours.

so if previous ip and hour was  125.45.67.32  1:00
and new visitor's ip and hour is 125.45.67.32  2:50
since the ip is same and hour is <= 3, so counter should not be incremented.
if hours is >= 3, then incremet it.

if new ip and hour is 187.56.43.34  2:57
since ip is different, so increment it.

if you have any ?. please ask. I am appreciated it. Thank you !
0
 

Author Comment

by:mcs436_2
ID: 1206742
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj           ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206743
change philmike's
 if( -e "log.dat" ){
to
 if( -e "log.dat" && -M "log.dat" < 3/24 ){
And as martinag suggests. do a chomp and flock
0
 

Author Comment

by:mcs436_2
ID: 1206744
But I don't know how to put those together in perl
can someone give me complete code. I will increase points to 220
Thank you !
0
 

Expert Comment

by:tribal9
ID: 1206745
Remember..for people with dialup, IP changes every time you sign on.  Especially for people on AOL who get booted twice an hour <g>.  So if you wanted to strictly count one person per day, it would get a bit harder.

Let me know if this is what you want, and I'll try and give you a rough example of how you could do it (cookies would be easiest..but you mentioned you didn't want to do that).

tribal9


0
 

Expert Comment

by:hap
ID: 1206746
You can find lots of free scripts for web site statistics right here:

http://www.freeperlcode.com/guide/Web_Site_Statistics/
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now