How can I in a php file get the content of a txt file every 30 seconds?

I have a txt file that contains usernames every row.

Like this:

username1
username5
username3.

what this php file will do is to check if the username-cookie is the same with the first line of this txt file. Than if thats the case I will redirect the user to another page. And if not, we will just keep waiting. This can be done with file_get_content, but how can I repeat the checking process every 30 seconds, because the txt files content will change every 30 seconds.

Also in another php file, I want to delete the first line and so the txt will become:

1st row: username5
2nd row: username3

(This is why I need to refresh the page, because after an amount of time, the file will change)
I know how to delete a line in txt, but I dont know how to do it without leaving a white line.
I want the txt file to begin with the next username after the one I just deleted.

So there you are, two questions. Any help is apreciated.

For the first question, I have been reading about Ajax. Would that help? Or do I really need a javascript solution to refresh the page( which I dont want to, because I dont want the user to see the page refreshing.)

1 Like

With js you will not need to refresh the page. With php alone, a page refresh is needed to process anything and change the html displayed.
But really, it may help if you explain why you want to do this. What is the purpose of the script, what is it for?
I get the feeling that whatever it is you are trying to do, there may be a better method of achieving it.
For example, in your other thread you asked how to count rows. When we discover the reason you want to count rows, it turns out that counting rows is not an effective way of achieving what you want.

2 Likes

True, here comes the explanation:

I have three files,

write.php where a user has 5 minutes to write into a .txt file.

read.php where users read .txt files.

and

inqueue.php where the user is waiting to be able to write in a txt file that someone else is writing in(the other user had 5 minute as I said).

When you are in read.php you have a button that says “I want to write to this”, and it forwards the user to write.php. At the beginning of write.php we check if the username is the same with the first line of the file_name_queue.txt. If yes the user can write for 5 minutes, at the end of the 5 minutes the user is reforward to read.php and than I will delete the first row of file_name_queue.txt. So that if another user is in inqueue.php, his page gets refreshed, and that page loads the new file_name_queue.txt where the new first line matches the username of this new user.

Don’t advice me to do this queue system with mysql, because I just want to achieve it with txt. No reason, I just want to do it as txt.

Here you go. Thats the story behind my question. Thanks for your helps Sam, you helped me quite a lot in learning php.

1 Like

There is no way to achieve that without JavaScript. However, JavaScript can be disabled and your entire system blows up. So it is all very brittle and problematic. Not to mention it sounds very convoluted. I think the term/process you’re searching for is called a semaphore. If you google on that term there are probably a boat load of better approaches.

2 Likes

I did a little bit of search about semaphore and mutex, but I am not sure thats what I am looking for.

I think I need a javascript solution. Could you be more precise about why you think my method is convoluted, I may improve it than.

1 Like

So how will it work if someone visits with JavaScript turned off?

2 Likes

It won’t, I will just show them a message expecting them to turn it on. And if they don’t, the site won’t let them in and thats it.
I myself never installed anything on my mac, and javascript works fine on my safira, so I will just hope it works for most of the users too.

I mean, I would love a better alternative, something that will work smoothly on every users browser. But you gotta do with what you have you know :smiley:

1 Like

Guess I’ll never be visiting then - I only turn JavaScript on for sites that work properly.

You haven’t been very constructive about the problem though.

1 Like

Where will the list of user names be originating from?

1 Like

whenever someone wants to join write.php, his name get written on it, if when he is already in write.php someone else is on top of the list, his name is written after him, and this goes so on.

The file_name_queue.txt is created whenever someone joins write.php for a file_name.txt.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.