Greetings from Pennsylvania. I am trying to add a counter to a web page but it is not cooperating! Directions are: place the count.php file and the hitcount.txt file on the server in the same directory as the web page. hitcount.txt file should contain a value like zero. For the include in the web page I use: <div class="footnote"> <?php include‘count.php’; ?> </div> Things I have tried: change permissions on both files to 777. Change the web site file extension to stml from html. The error logger on the server shows no new errors. I ran a install.php to install a forum on the server last week so I know php files will run in my server space. Could someone with knowledge of php please look at this code? $filename = "hitcount.txt"; // This is at root of the file using this script. $fd = fopen ($filename, "r"); // opening the file counter.txt in read mode $contents = fread ($fd, filesize($filename)); // reading the content of the file fclose ($fd); // Closing the file pointer $contents=$contents+1; // incrementing the counter value by one echo $contents; // printing the incremented counter value $fp = fopen ($filename, "w"); // Open the file in write mode fwrite ($fp,$contents); // Write the new data to the file fclose ($fp); // Closing the file pointer
What's up with that include? Shouldn't it be like this? <div class="footnote"> <?php include ("count.php"); ?> </div>
Thanks. It is still unhappy. I will try a perl counter; the problem is switching to .stml will require me to create dummy .html file to redirect it to the .stml I gather.
This counter works for me: $filename = "hitcount.txt"; $count = file_get_contents($filename); if ($count == null) $count = 0; echo $count; $count++; $handle = fopen($filename, "w+"); fwrite($handle, $count); fclose($handle); Code (markup):
I back like pesty fly. Thanks for the code. I am still dorking around try to get a counter to count. I see in O'REILLY "Programming PHP" they embed the php code into the html file so I am trying that but I do not see any count. Please look at the source at: http://www.bobbear.org/TestWebSite/TestWebSite.html
count.php is in the same directory as index.php and in index.php you want to "include" count.php, which open a file, read its content and display the value, right? So, to avoid misconfigurations, use this include __DIR__ . "/count.php"; Code (markup): Same for count.php on opening hitcount.txt. This should work but better if you can post the directory tree like, eg, web_directory ├── subfolder │ ├── counter.php │ └── hitcount.txt └── index.php Code (markup):
Thank you all. I found that <!--#include virtual='counter.php' --> works. New page: http://bobbear.netfirms.com/PHPTestWebSite2/PHPTestWebSite.shtml see try 0. I am going to stay away from the server side, they may have cookies there but it is to complex there for me. I am going back to embedded C programming and stay there!