Hi there,

I'm writing a PHP counter and am having some trouble getting it to work!

Here is my code stored in counter.php:

$logpath = ("counter.txt");

$pad = 5;

if (file_exists($logpath)) {	
$count = file_get_contents($logpath);
}else{ 
$count = 0;
}

$count++;

$file = fopen($logpath, "w"); // the 'w' indicates writing mode
if (flock($file, LOCK_EX)) { // do an exclusive lock
fwrite($file, $count); // write out the updated count
flock($file, LOCK_UN); // release the lock
}
fclose($file);

$count = str_pad($count, 5, '0', STR_PAD_LEFT);
echo $count;

The counter.txt file simply contains 0

The script used to call the script is on the index.php page:

<?php include_once("includes/phpcounter/counter.php"); ?>

When sent to the server and tested, I get these replies:

Warning: fopen(counter.txt): failed to open stream: Permission denied in /home/public_html/TEST/includes/phpcounter/counter.php on line 30

Warning: flock(): supplied argument is not a valid stream resource in /home/public_html/TEST/includes/phpcounter/counter.php on line 31

Warning: fclose(): supplied argument is not a valid stream resource in /home/public_html/TEST/includes/phpcounter/counter.php on line 35
00001

As you can see, THEN the warnings are followed by what I want to see - the counter!

Whoever can help, I'd be eternally grateful!

Thank you!

Best,

rach

    For what are the () at $logpath?

    Maybe it works when write it without them.

      The first error message tells you what the problem is. The user profile which runs your web server does not have permission to access the counter.txt file.

      The easiest thing to do is to determine what user your web server runs as (possibly "nobody" or "apache" and make them the owner of the counter.txt file, or alternatively give everyone full access to that file "chmod 777 counter.txt".

        Hey guys, thanks for the quick replies!

        The () were just a try out of curiosity - seems that you can script with or without the () - doesn't seem to effect it in this case anyway!

        Ok, Im with you on the permission denied situation - you see, this is a server I have ftp access to for re-designing a site for a client, so the server type information wouldn't be known by them - is there a way I can figure this out for myself?

        I'm a little unsure as too how I alllow this access for all - you mentioned something about chmod 777 counter.txt?

        May I ask for your assistance a bit further?

        Many thanks - I really want to get to understand this whole php thing!

        rach

          No worries guys!

          Did a wee google search and know exactly what you're on about now! I use fetch and totally forgot about all that permissions malarky! It all works now, I started from the root folder and worked my way into the individual file ticking the boxes to = 777 - fantastic advise, the murky waters are now clear!

          As I am more into design than coding, if I can ever return the favour to you guys - throw you a few cool websites, offer critique on any work as an outsider, I'd be more than happy to do so!

          Many many thanks again!

          rach

            Write a Reply...