I've been looking for hours for a solution to this problem.
I have a panel which takes everything in a given directory and displays it. I would like it to do this every 10 seconds because right now you have to refresh your entire page.
So when I do this
GetLogs(); sleep(5); GetLogs();
Instead of getting the directories, displaying them, waiting 5 seconds, and then displaying them again, it simply does GetLogs();, waits for 5 seconds then does it again, and then load my page. I have seen people use AJAX but it's a bit out of my league for this problem I think. I've read that changing my apache settings or php settings would resolve this problem but I have no idea how. I also looked into session_write_close and session_start but these do nothing, These functions are being called from within this php file. please help me!
I was stupid and for some reason thought I could use a server side language to sleep client-side, my bad. Could someone spoonfeed me an ajax call please?
If you just want to refresh the page every 5 seconds you could use
<?php
header("Refresh: 5");
?>
This must be sent before any other output
This question is a very common XY problem asked by new developers.
PHP runs on the server. The web browser has no knowledge of what's on the server. To the web browser, the server is a black box: the web browser sends it text, the server does server stuff, and the server sends text back to the browser. Typically the response text will describe a webpage in the form of HTML. But the web browser has no way of knowing how that HTML was generated: it can't see the underlying PHP.
This process has a set order:
You can't really change this order. Technologies like ajax work around this by doing additional processing in the web browser and making additional requests. This is handled by JavaScript. For example:
This is a relatively simple exchange. In practice, many web pages will get more complex. For example, the server might respond with JSON instead of HTML; it will then be up to the JavaScript in the browser to do something useful with the JSON, such as generating HTML.
In your particular case, ideally the flow should look like this:
<script>
tag.<script>
tag in the HTML and requests it from the server.setInterval
to run a JavaScript function every 10 seconds.json_encode()
instead of outputting HTML.JSON.parse()
.What's important to note here is that there isn't a fluent communication tunnel between the web browser and the server. You can't call a JavaScript function from PHP, and you can't call a PHP function from JavaScript. Each time one needs to send a message to the other:
Think of it like this: Say there are two people, Alice and Bob. Alice and Bob want to exchange messages. The only way they can do that is via snail mail. But Bob doesn't have any stamps. So each time they want to exchange messages, Alice has to initiate the conversation. She sends Bob a letter, including an extra stamp so Bob can send a response. Bob has to wait for Alice's letter before he can send a response. Once he responds, that's it--he can't edit the letter while it's in transit. If he wants to make changes, he has to wait for Alice to request them.
In the process of searching for an answer to your question, you may encounter instructions to disable buffering or refresh the page repeatedly. While these may technically give the desired result in your particular case, they aren't really good solutions. You should look into solving the actual issue at hand: you need to find a way to get ajax working with JavaScript in the web browser.