Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

easy point for all experts

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
mcs436_2
Asked:
mcs436_2
  • 7
  • 5
  • 4
  • +5
1 Solution
 
sybeCommented:
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
 
mcs436_2Author Commented:
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
 
martinagCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
philmikeCommented:
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
 
martinagCommented:
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
 
mcs436_2Author Commented:
sorry ! I have to reject your answer because I said already, I don't want a cookies

Thank you for your effort
0
 
mcs436_2Author Commented:
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
 
martinagCommented:
Use cookies.

Martin
0
 
martinagCommented:
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
 
ozoCommented:
Does 'care about' mean 'count'?
0
 
martinagCommented:
Not necessarily, but it could. That's mcs436_2's decision (was it necessary to select such a user name? :-)

Martin
0
 
ozoCommented:
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
 
tpryorCommented:
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
 
mcs436_2Author Commented:
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
 
ozoCommented:
So, you've decided you want to check only the previous IP address, and not all IP addresses in the past 3 hours?
0
 
mcs436_2Author Commented:
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
 
mcs436_2Author Commented:
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj           ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
0
 
ozoCommented:
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
 
mcs436_2Author Commented:
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
 
tribal9Commented:
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
 
hapCommented:
You can find lots of free scripts for web site statistics right here:

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 5
  • 4
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now