php counter, file writing problem.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arrotino
Forum Newbie
Posts: 4
Joined: Mon Jul 18, 2005 6:08 am

php counter, file writing problem.

Post by arrotino »

Hi everyone. I'm new to PHP and was trying to implement a simple pageviews counter that increments pageviews if last IP that visited the page is different from current IP. (sorry for my bad english, I hope you got the idea).
I thought I had to use a file to store pageviews and last IP.

My code partially works. at the first visit the txt file is created and looks like this:
1
82.50.blah.blah

at the second pageviews the counter file looks like this
1

82.47.blah.blah

where's that more line coming from? here's the code.

Code: Select all

$filename = 'contatore.txt';
$user_ip = getenv("REMOTE_ADDR");

//if file exists
if (file_exists($filename)) {
	$fp = fopen ($filename , 'r');
	$cnt = fgets ($fp , 1024);
	$last_ip = fgets($fp , 1024);
	fclose($fp);
	print ("page viewed $cnt times. your IP is $user_ip");

	if ( $last_ip != $user_ip ) {
		$fp = fopen ($filename , 'w');
		$cnt++;
		fwrite($fp , $cnt);
		fwrite($fp , "\n");
		fwrite($fp , $user_ip);
		fclose($fp);   			}
} 

//if file doesn't exist, create it and set views to 1.
else {
	$fp = fopen ($filename , 'w');
	$cnt=1;
	fwrite($fp , $cnt);
	fwrite($fp , "\n");
	fwrite($fp , $user_ip);
	fclose($fp);
	print ("page viewed $cnt times. your IP is $user_ip");
}
Hope someone will be so kind to help. It's like 2 days I'm working on this and it's driving me crazy :|

Thanks in advance.
Gabriele.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
Trim data before using/writing it.

Also this counter does not work as supposed to because visitor_2 will overwrite data of visitor_1.

Code: Select all

<?php

$filename = 'cnt.txt';
$user_ip = getenv("REMOTE_ADDR");

//if file exists
if (file_exists($filename)) {

    $fp = fopen ($filename , 'r');
    $cnt = fgets ($fp , 1024);
    $last_ip = fgets($fp , 1024);
    fclose($fp);

    $cnt=trim($cnt);
    $last_ip=trim($last_ip);

    print ("page viewed $cnt times. your IP is $user_ip");

    if ( $last_ip != $user_ip ) {
        $fp = fopen ($filename , 'w');
        $cnt++;
        fwrite($fp , $cnt);
        fwrite($fp , "\n");
        fwrite($fp , $user_ip);
        fclose($fp);
    }

}

//if file doesn't exist, create it and set views to 1.
else {

    $fp = fopen ($filename , 'w');
    $cnt=1;
    fwrite($fp , $cnt);
    fwrite($fp , "\n");
    fwrite($fp , $user_ip);
    fclose($fp);
    print ("page viewed $cnt times. your IP is $user_ip");
}

?>
djot
-
Last edited by djot on Mon Jul 18, 2005 6:49 am, edited 1 time in total.
arrotino
Forum Newbie
Posts: 4
Joined: Mon Jul 18, 2005 6:08 am

Post by arrotino »

thanks a lot, it now outputs correctly to the file. but the counter doesn't increase :(
I'll try to work on that now.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
The counter does not incease, because the data is a string that can't be increased
e.g. 5\n

See my posting above, I edited it to work properly.

djot
-
arrotino
Forum Newbie
Posts: 4
Joined: Mon Jul 18, 2005 6:08 am

Post by arrotino »

the code now works. I substituted

Code: Select all

$cnt++;
with

Code: Select all

$cnt=$cnt+1;
looks weird btw.

I'm gonna have a look at your code too. thanks a lot, you saved me. :D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

arrotino wrote:the code now works. I substituted

Code: Select all

$cnt++;
with

Code: Select all

$cnt=$cnt+1;
looks weird btw.

I'm gonna have a look at your code too. thanks a lot, you saved me. :D
They both mean exactly the same thing ;)

Code: Select all

$var = $var.'word';
$var .= 'word';

//----
$var = $var + 1;
$var += 1;
$var++;
++$var;

//----
$var = $var + 2;
$var += 2;

//----
$var = $var * 3;
$var *= 3;

//----
$var = $var / 4;
$var /= 4;

//---
$var = $var % 2;
$var %= 2;
Umm... you get the idea.. I must be bored lol :P
arrotino
Forum Newbie
Posts: 4
Joined: Mon Jul 18, 2005 6:08 am

Post by arrotino »

I know they're the same, that's why I stated it was "weird".
edit: probably the "++" form cannot work with a char? I don't know. I'm just guessing
Post Reply