PDA

View Full Version : PHP: Error I can't figure out



Ben
November 4th, 2003, 20:14
Hey,

I'm learning php, and just for a little practice I tried to make a small page hit counter. Only it tells me there's a parse error on line 33. I can't figure out what's wrong. Anyone have any ideas?

Here's line 33:

if($count >= 0) {

...and here's the rest of the script:

<?php

/*
hit counter
count.php
by Ben

This counter only counts
TOTAL hits. It does not
distinguish between unique
and continuing visits.
*/


///////////////////////////////////
//[Leave this section alone] //
//get the file name //
//$id = $PATH_TO_DIR.$id; // <--forgot to take that out
///////////////////////////////////

//This is the beginning hit count.
//You can cheat by changing it.
//I'll leave it at zero
$count = "0";

//Don't change anything except
//the starting hit count!

//the count file holds the number of hits.
$count_file = "count.txt"

//add a count
if($count >= 0) {
$count++;
}

//this is where all the
//good stuff gets written

$file = fopen($count_file, "w+");
fwrite($file, $count);
fclose($file)

?>

I forgot to mention, It's called to the page you want to count hits on using PHP includes.

Thanks!
Ben

keith
November 4th, 2003, 23:30
change:
$count_file = "count.txt"to:
$count_file = "count.txt";line 30 i suppose?

keith
November 4th, 2003, 23:32
entirely revised:
<?php

/*
hit counter
count.php
by Ben

This counter only counts
TOTAL hits. It does not
distinguish between unique
and continuing visits.
*/


///////////////////////////////////
//[Leave this section alone] //
//get the file name //
//$id = $PATH_TO_DIR.$id; // <--forgot to take that out
///////////////////////////////////

//This is the beginning hit count.
//You can cheat by changing it.
//I'll leave it at zero
$count = "0";

//Don't change anything except
//the starting hit count!

//the count file holds the number of hits.
$count_file = "count.txt";

//add a count
if ($count >= '0') $count++;

//this is where all the
//good stuff gets written

$file = fopen($count_file, "w+");
fwrite($file, $count);
fclose($file);

?>among many other potential bugs, it looks to me like the count will always be set to "1".

spec
November 5th, 2003, 00:37
Keith is correct on both issues.

Rule of thumb if you receive a parse error and you do not see an error on the line in question, look to the line above it and make sure it has a semi colon.

Also your counter wouldnt actually count. You see you need to open the counter file first. then retrieve the number in that file, add to it, then write to the file. write now you are just writing the same number to the file every time it is run.

Ben
November 5th, 2003, 15:42
Thanks for your help, Keith & spec.

I know how to open and write a variable to a file, but how do retrieve the number from the file?

Thanks!
Ben

keith
November 5th, 2003, 16:40
try this, it should work:
<?php

// name file to use
$count_file = '/path/to/counter.txt';

// read count from file
if ($read_file = fopen($count_file, 'r')) {
flock($read_file, LOCK_SH);
$count = fread($read_file, filesize($count_file));
flock($read_file, LOCK_UN);
fclose($read_file);
}

// set new count
$count = intval($count);
$count++;

// write new count to file
if ($write_file = fopen($count_file, 'w+')) {
flock($write_file, LOCK_EX);
fwrite($write_file, $count);
flock($write_file, LOCK_UN);
fclose($write_file);
}

// show count
echo $count;

?>

Ben
November 5th, 2003, 16:43
Thanks keith!

keith
November 5th, 2003, 16:46
no problem...

take another look though, i typoed 'filesize' to read 'fileseize'. whoops, fixed it. i added file locking as well.

Ben
November 5th, 2003, 19:22
Originally posted by keith
no problem...

take another look though, i typoed 'filesize' to read 'fileseize'. whoops, fixed it. i added file locking as well.
What's file locking? Does that prevent people from reading it off of remote sites?

keith
November 5th, 2003, 20:07
file locking protects it from being corrupted when two or more people access the file at the same time.

Ben
November 5th, 2003, 20:30
Originally posted by keith
file locking protects it from being corrupted when two or more people access the file at the same time.
Ohhh ok.