Click to See Complete Forum and Search --> : simple access counter - doesn't work
discus
07-23-2005, 09:31 PM
Hi,
Could someone tell me why this code doesn't work?
<script language="php">
//from 'A Programmer’s Introduction to PHP 4.0 W. J. Gilmore'
// script: simple access counter
// purpose: uses a file to keep track of visitor count.
$access = "hits.txt"; // name this file whatever you want
$visits = @file($access); // feed file into array
$current_visitors = $visits[0]; // extract first (and only) element from array
++$current_visitors; // increment visitor count
$fh = fopen($access, "w"); // open "hits.txt" and place file pointer at beginning of file
@fwrite($fh, $current_visitors); // write new visitor count to "hits.txt"
fclose($fh); // close filepointer to "hits.txt"
</script>
I have PHP4.3.11 working on IIS.
My localhost is C:\Inetpub\wwwroot which contains hits.txt and html file with this script embeded.
I placed only '0' in the hits.txt file (the same result with blank).
I opened (and closed) html file from browsers adress bar for several times (with:
http://localhost/testfile.html) and no changes in the hits.txt file.
Thanks for your help
bathurst_guy
07-23-2005, 10:28 PM
This one works. For some reason I cant access php.net atm so i cant remember the permissions to open a file for both reading and writing so if you know this then make some mods and lose a few lines of code...
<?php
$DOCUMENT_ROOT=$HTTP_SERVER_VARS['DOCUMENT_ROOT'];
@ $fp = fopen("$DOCUMENT_ROOT/../hits.txt","r");
$current_visitors = file("$DOCUMENT_ROOT/../hits.txt");
fclose($fp);
@ $fp = fopen("$DOCUMENT_ROOT/../hits.txt","w");
$outputstring = $current_visitors[0] + 1;
fwrite($fp, $outputstring);
fclose($fp);
?>
Make sure that hits.txt has read and write permissions, I just set it as 777 and put it in my home directory / - above public_html (this way no one can access and edit it from the Internet and you dont have to worry)
discus
07-24-2005, 03:55 PM
Unfortunately, your script doesn't work too.
1) I'm not quite sure what permissions you talking about.
In my script I don't need permission for reading in fopen but just for writing (with previous file contents erased): " w - Write only. The file pointer is placed at the beginning of the file, and the file contents are erased."
Your script is a bit different, but permissions (r, w) are ok to me.
If you meant: is the hits.txt a read-only file - no, it's not.
2) what do you mean by 'set the file as 777' ??
3) Since my $DOCUMENT_ROOT variable is C:\Inetpub\wwwroot and it actually contains the hits.txt file, what is the meaning of /../ in your script.
It seems more logical to be "$DOCUMENT_ROOT/hits.txt" - or you've put that just as a placeholder ?
Here is the countertest.html file, with the script:
<html>
<head>
<title>Counter test</title>
<?php
$DOCUMENT_ROOT=$HTTP_SERVER_VARS['DOCUMENT_ROOT'];
@ $fp = fopen("$DOCUMENT_ROOT/../hits.txt","r");
$current_visitors = file("$DOCUMENT_ROOT/../hits.txt");
fclose($fp);
@ $fp = fopen("$DOCUMENT_ROOT/../hits.txt","w");
$outputstring = $current_visitors[0] + 1;
fwrite($fp, $outputstring);
fclose($fp);
?>
</head>
<body>testing</body>
</html>
Is that ok ?
I start it through http://localhost/countertest.html
Thanks again
CompGeek01
07-24-2005, 06:56 PM
I'm not sure about the script, I'll take a minute to look over it. As for permissions, 777 is a format commonly used in Unix systems.. each digit stands for a person or group of people who have access and what kind of access they're allowed.
If you're interested in learning how they work (pretty easy IMO) go to the very bottom of this page under 'Strange Numbers' ::
http://www.perlfect.com/articles/chmod.shtml
*edit*
In case you don't want to read, 777 is univerally read/write/execute for everyone.
CompGeek01
07-24-2005, 07:07 PM
<html>
<head>
<title>Counter test</title>
<?php
$DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
$current_visitors = file_get_contents( $DOCUMENT_ROOT . "/hits.txt");
$fp = fopen( $DOCUMENT_ROOT . "/hits.txt" , "w" );
$outputstring = $current_visitors + 1;
fwrite($fp, $outputstring);
fclose($fp);
?>
</head>
<body><? echo $outputstring; ?></body>
</html>
Works for me as long as the file is writable by "world".
discus
07-24-2005, 09:44 PM
Huh, this script doesn't work neither. No echo, no change in hits.txt.
I've forgot to mention that my PHP works in ISAPI mode (not CGI), if that does matter.
As for 777, I use IIS (Win, not UNIX), and from your link I see these 'Strange Numbers' are unix permissions.
It's odd, but my other simple testing scripts (without fopen and fwrite) work fine.
Thanks
p.s.
What's the meaning of ' writable by "world" '?
CompGeek01
07-24-2005, 09:53 PM
Sorry, I honestly have no idea how permissions on a Windows machine works. It's weird you're not getting any errors though.
You might try adding:
error_reporting(E_ALL);
to the beginning of the file and hopefully it'll show the error message that is causing the script not to work.
bathurst_guy
07-25-2005, 01:04 AM
3) Since my $DOCUMENT_ROOT variable is C:\Inetpub\wwwroot and it actually contains the hits.txt file, what is the meaning of /../ in your script.
It seems more logical to be "$DOCUMENT_ROOT/hits.txt" - or you've put that just as a placeholder ? Yes, placeholder, obviously I dont know the exact organisation of your site...
Now it should work......
There must be something else your doing wrong.
(I'm just going to speak out loud through my 'troubleshooting')
So the file location is correct;
The file permissions are correct;
The code is correct;
Is the file extention correct? .php (or is your server set up to process other extentions?)
BeachSide
07-25-2005, 12:59 PM
This will tell you real quick if you have permission to write to the file or not and it has been tested and works...
<html>
<head>
<title>Counter test</title>
<?php
$filename = 'hits.txt';
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$current_visitors = file_get_contents($DOCUMENT_ROOT . '/' . $filename);
if (is_writable($filename)) {
if (!$fp = fopen($DOCUMENT_ROOT . "/hits.txt" , "w")) {
die("Cannot open file {$filename}");
} else {
$outputstring = $current_visitors + 1;
fwrite($fp, $outputstring);
fclose($fp);
}
} else {
die("The {$filename} file is NOT writable!");
}
?>
</head>
<body><?=$outputstring;?></body>
</html>
discus
07-26-2005, 12:15 PM
With your help guys, things are getting better.
Extension really does matter.
Firstable, I've noticed that html files don't work on my localhost & IIS (Internet Information Services) at all.
So, I've changed extension for my countertest file to .php .
Now CompGeek01's script gives '1' on page (remains 1 after multireloads), but with no change in the hits.txt :
<html>
<head>
<title>Counter test</title>
<?php
$DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
$current_visitors = file_get_contents( $DOCUMENT_ROOT . "/hits.txt");
$fp = fopen( $DOCUMENT_ROOT . "/hits.txt" , "w" );
$outputstring = $current_visitors + 1;
fwrite($fp, $outputstring);
fclose($fp);
?>
</head>
<body><? echo $outputstring; ?></body>
</html>
BeachSide's script gives the message 'Cannot open file hits.txt', of course without any record in the hits.txt :
<html>
<head>
<title>Counter test</title>
<?php
$filename = 'hits.txt';
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$current_visitors = file_get_contents($DOCUMENT_ROOT . '/' . $filename);
if (is_writable($filename)) {
if (!$fp = fopen($DOCUMENT_ROOT . "/hits.txt" , "w")) {
die("Cannot open file {$filename}");
} else {
$outputstring = $current_visitors + 1;
fwrite($fp, $outputstring);
fclose($fp);
}
} else {
die("The {$filename} file is NOT writable!");
}
?>
</head>
<body><?=$outputstring;?></body>
</html>
I think we're getting closer to the solution.
Thanks alot
BeachSide
07-26-2005, 03:01 PM
If it cannot open it then you have permission problems.
What version of windows are you running?
discus
07-26-2005, 09:03 PM
xp pro/sp2 (IIS v5.1)
discus
07-26-2005, 10:18 PM
I wish to take this opportunity to thank bathurst_guy, CompGeek01 and BeachSide for their great help.
Yes, the problem is solved.
I've changed permissions for hits.txt in windows (file properties/security tab/write to everyone).
All scripts are working now.
One last Q: How can I make my PHP&IIS to work with html files (in order to test them) ?
Or, maybe, I don't have to do that, because (I'm just guessing), I can test any html file as a php, and then just revert it to html before putting it on a server.
T
bathurst_guy
07-26-2005, 10:21 PM
if you have php code in your page, leave the file extension as .php on the server.. depending on server setup you can have htm files processed as php and it will work but as i said depending on server setup. I find it easier to just have them saved as .php then if i happen to change servers and the new one doesnt allow htm processing then i dont have to rename all the file extentions or have to wait for them to change the settings....
bokeh
07-27-2005, 02:51 AM
Sorry, I honestly have no idea how permissions on a Windows machine works. It's weird you're not getting any errors though.
You might try adding:
error_reporting(E_ALL);
to the beginning of the file and hopefully it'll show the error message that is causing the script not to work.
In windows all files have the equivalent of 0777 permissions so unless there is a base directory restriction in place all have read, write and execute permission. On the second point, that solution will only work if display errors is turned on in php.ini. If it is not turned on then it is not possible to do so from the script.