views:

102

answers:

4

I set up a hitcounter so that each time someone goes to my site, one of 7 different sidebars loads up. There is a file called counter.php that writes to a text file called hitcounter.txt. All the references to files seem to be relative but when I moved them to a new directory at my new host I got this error instead of a happy hit counter:

<b>Warning</b>:  fopen(hitcounter.txt) [<a href="function.fopen">function.fopen</a>]: failed to open stream: Permission denied in <b>/usr/local/apache/sites/MY-SITE/counter.php</b> on line <b>5</b><br>

Counter.php is pasted in its entirety below, line 5 is the first reference to fopen, both counter.php and hitcounter.txt have 775 permissions, same as they did on the old host.

What am I doing wrong? I'm obviously missing something really simple and embarrassing, so feel free to give me any scorn or abuse with while helping me out.

counter.php:

<?php
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
if ($fp) {
fputs($fp , "$hits[0]");
fclose($fp);
}


if($hits[0]<=1)
    {


    $random_number=0;

    }

else if($hits[0]>1 && $hits[0]<=2)
    {


    $random_number=1;

    }


else if($hits[0]>2 && $hits[0]<=3)
    {


    $random_number=2;

    }
else if($hits[0]>3 && $hits[0]<=4)
    {


    $random_number=3;

    }


else if($hits[0]>4 && $hits[0]<=5)
    {


    $random_number=4;

    }

else if($hits[0]>5 && $hits[0]<=6)
    {


    $random_number=5;

    }

else if($hits[0]>6 && $hits[0]<=7)
    {


    $random_number=6;

    }


else if($hits[0]>7 && $hits[0]<=8)
    {


    $random_number=7;

    }


else if($hits[0]>8 && $hits[0]<=9)
    {


    $random_number=8;


    if($hits[0]==9)
    {
    $count_my_page=("hitcounter.txt");
    $fp = fopen($count_my_page , "w");
    $hits[0]=0;
    fputs($fp , "$hits[0]");
    fclose($fp);

    }
    }

?>
+1  A: 

First, you don't need 775 permissions. You need 666 permissions on the hitcounter.txt. The PHP file can be 644.

The web server probably isn't a member of the group, depending on the host, so you'd need to give the 'Everyone' group write permissions.

The "Execute" bit is needed for folders but not for individual files, since they are not being executed by the OS.

So you know, the 775 is : Owner, Group, Everyone

  • Owner = Read + Write + Execute

  • Group = Read + Write + Execute

  • Everyone = Read + Execute

666 means

  • Owner = Read + Write

  • Group = Read + Write

  • Everyone = Read + Write

644 means

  • Owner = Read + Write

  • Group = Read

  • Everyone = Read

Chris Thompson
Thanks a lot. You all answered my questions just fine but Chris was fastest!
pg
A: 

Try setting permissions to 777. If that doesnt work, could be your host doesnt allow file manipulation on the server

Ozzy
A: 

Check your include_path to make sure it has a period (current directory) in it. Not sure why it wouldn't, but you never know if your host is wonkey. You can print out your include path from a PHP script using ini_get:

ini_get('include_path');

Other than that, you probably need different file permissions. Here's a useful site describing a lot of information on file permissions.

Andrew Dunkman
+2  A: 

It probably has something to do with permissions. Set it to 777 and see what happens. If apache runs with own permissions and is not part of your this might be the reason, but I have a couple of suggestions:

  • use file_put_contents/file_ get_ contents for simple read/writes!
  • please use $random_number = rand(0,8) OR mt_rand(0,8) if possible instead of these countless lines and as a bonus get rid of all the file reading/writing

Good luck!

Update

Nothing beats a nice example:

<?php 
  $random_number = mt_rand(0,8);
  file_put_contents("hitcounter.txt", $random_number); /* dont know if you still need it */
?>

If you really want to (btw. NOT random!):

<?php 
  $file = "hitcounter.txt";
  $number = (int)file_get_contents($file);
  $number = ++$number % 9;
  file_put_contents($file, $number);
?>
merkuro
+1 - The file_put/file_get commands do make file processing a LOT easier
Chris Thompson