I have a php server file and an HTML client file, the HTML file sends ajax requests to the server to retrieve data every 500 ms , although this works as expected, resulting in high memory and processor usage on the client device.
Php
if(isset($_POST['id']) && $_POST['id'] != '' ) { $id = $_POST['id']; $select = $con->prepare("SELECT * FROM data WHERE id=?"); $select->bind_param('s', $id); $select->execute(); $result = $select->get_result(); while($row = $result->fetch_assoc()) { echo $row['column 1'] . "\t" . $row['column 2'] . "\n"; } }
AJAKS
function send(){ var formdata = new FormData(), id = document.getElementById('id').value; formdata.append('id', id); var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); xhr.open('post', 'server.php', true); xhr.send(formdata); xhr.onreadystatechange = function(){ if(xhr.readyState == 4 && xhr.status == 200){ console.log(xhr.responseText); } } } setInterval(function(){send()}, 500);
I would like to find an alternative solution for ajax, instead of sending numerous requests to the server and receiving the same data most of the time, it would be much more effective if the server could interact with the client when changing or updating data.
I cannot use PHP Socket or HttpRequest medthods as they are not installed on my hosting server and I am not sure if it works later. The only way I can think of is to use SESSIONS .
according to this, the PHP server stores all user sessions in the same directory on the server, so it may be possible to change the session variables for a specific user directly to a file. however, the problem is that the data in these files is serialized, and I'm not sure how to de-serialize the data and re-serialize it, and then save the new data!
Even if I managed to find a way to store updates in the session file, I still need to use setInterval to listen for the change of the session variable every 500ms , although it is not ideal, but it will be much better than using XMLHttpRequest in terms of memory and processor usage.
So what's the best way to do this? Any help would be greatly appreciated.
UPDATE:
I realized that SESSION does not work, because it can only be read by the server, and not by the client, so I need to send an ajax request to the server to get the variables that I was trying to avoid.
I tried a lengthy poll, but I had a lot of problems with it, flush and ob_flush() do not work on my server, and I can not change the ini settings. When trying an infinite loop, I cannot make it break when data changes:
if(isset($_GET['size']) && $_GET['size'] != '') { $size = (int)$_GET['size']; $txt = "logs/logs.txt"; $newsize = (int)filesize($txt); while(true) { if($newsize !== $size) { $data = array( "size" => filesize($txt), "content" => file_get_contents($txt)); echo json_encode($data); break; } else{ $newsize = (int)filesize($txt); usleep(400000); } } }
it continues to move back and forth, even if the size of the logs.txt does not change! how can i make it break and echo data to increase size?
UPDATE 2:
it turned out that php cache has a file size when calling the filesize() method, so the specified loop will work indefinitely, the solution for this is to use the clearstatcache() method, which will clear the saved file size cache, allowing the loop to cancel file size changes.