Help please

True I’m very new to PHP, I created this code as a counter called c_uc1.php :

<?php $count = intval(file_get_contents('c_uc1.txt')); file_put_contents('c_uc1.txt', ++$count); ?>

using

<?php require('./c_uc1.php'); ?>

to call it within index.php

using similar code just the text file name has been changed on the index.php

It works very well on a Wamp server and has never failed there, true it’s simple, my problem is when uploaded to a hosting site, there are two pages, the first has never failed, the second with the above code, works then stops after a few attempts, I’m guessing a register or buffer might be the problem and needs a reset after each running of the code, as I said new to php and without knowing the keyword to search for I’m lost, this simple bit of code has cost me many sleepless nights, any ideas or help will be gratefully accepted.

Might be a server setting.

What does the counter do, just take the number that’s in the text file and increment by 1? and what are you using the counter for? maybe there’s a better way of doing it.

Yes increments by 1 and used as a simple visitor counter for personal information, while learning and developing a site. The display part is as follows:

Visitor  

The above code displays it on the page in a browser. With PHP I’m experimenting, normally I learn the fundamentals and building blogs of a subject that ways I have the knowledge to work out what might be the problem, with php I have not been able to find the very basics and thus have landed in alien world where my tricorder translates the code without understanding how it works!

Sorry about asking the question, but since writing it I have found the code is correct and runs 100%, It’s the browser not updating, but using a previous call and showing it, if I clear the history and cache it works, but seems to stop after a few attempts, but the files and data have updated, if I continue reloading it shows the same figure every time until the history is cleared, then loading shows the updated figure. Something totally different to look into, a trap to frustrate using firefox, have to check with other browser.

Tried all the browsers and they all do the same, pulling the file from history and displaying it, but the code is run, when history is cleared it shows the current update information. That’s why I’ve been going in circles, with the frustration building!

Inside your code, are you changing folders at all? You are using “./” to access the file.
This means back up one fold and then load the file. Perhaps something is getting lost there?

Also, using text files to save data can be an issue if there are more that one person accessing the
site at the same time or if you refresh your browser. Timing issues could be a problem. If a browser
is open to the site and writing to it, another session can interfere with it. Lastly, the server could be
caching the writes of the file since you have no timestamps used in the data. Could just be some
timing of how the browser loads and rewrites the file.

Are you trying to use HTML to keep timestamps of users? Would work better using a database
especially if you are planning on a lot of users logging in at a time. Text files would most likely not
work for that.

True what you say, all taken in, @ the moment on a new learning curve, just don’t like not understanding when something happen that’s unexpected! I know database would be better but not knowing why? now that I’ve got this far would be worse for me personally, once I understand whether the code is good/bad, work or not, etc. I can move on till then will persist,. once the understanding is there, it won’t be forgotten.

Well, yes, I understand wanting to solve it! So, how have you debugged it so far?

Usually, to debug a problem, you temporarily enter some stops that show your current data and
then stops the code at that point. If the data is what you expected, then, move it down the code some.

So, in your PHP code, you could add something like:
[php]

<?php $count = intval(file_get_contents('c_uc1.txt')); echo "Current count=" . $count; die("
New count =" . ++$count); file_put_contents('c_uc1.txt', ++$count); ?>

This will stop the code just before writing the data and you can verify what this part of the code is
creating. One step at a time…
[/php]

thank you, will try this, but not tonight, will sleep on it. :’(

Sponsor our Newsletter | Privacy Policy | Terms of Service