开发者

jquery - refresh the page when new data stored in to the table across the browser sessions

开发者 https://www.devze.com 2022-12-19 04:12 出处:网络
Environment : PHP/MySQL/Jquery I would like to refresh the page (ex: index.php) when new data stores in to the table (ex : new_entry_table) checking for every one minute. The same page will be opene

Environment : PHP/MySQL/Jquery

I would like to refresh the page (ex: index.php) when new data stores in to the table (ex : new_entry_table) checking for every one minute. The same page will be opened in many machines(pc-browsers) at a time all should get refresh when new data arises.

using jquery and database check i tried the following:

setInterval(function(){
$.post("new_data_check.php", function(data) { 
   if(data > 0){ 
       $("#container").fadeOut('fast').load(location.reload());
   } 
   });
   return false;
},60000);

new_data_check.php:

: checks 开发者_运维问答 for new data in the test_db where newdata_field=0 ;

: If any new data arises echo "1"; And update the newdata_field=1; to stop constantly refreshing the page.

: else echo "0";

: Just for a trigger

So the above jquery code checks new_data_check.php every 1 min for the trigger , if any trigger arises it will refresh the container.But its getting refresh only one opened session other opened sessions(in other browser or other pc) not getting refreshed.

Suggest a better way to do this. Thanks.


Your new_data_check.php file will need to keep track of all the users that are independently viewing the page.

If not, this will happen:

  • User 1 and User 2 are both logged into the site.
  • User 1 does the check 5 seconds before User 1, there is new data.
  • User 1 completes his request. The server clears the new_data bit.
  • User 2's request arrives 5 second later. Hedoes his request, but at this point, User 1 has set the new_data field to 0. His data is stale, but User 1 has effectively co-opted him.

By the looks of it, you're not supplying it with any information that would identify the user - unless the server is determining that using the IP address. If that's the case, remember that it's likely everyone behind a small network is going to have the same external ip address. So it can't tell people at an office apart.

What you could do is generate some kind of unique hash for each session and pass that off to the browser. The javascript can then provide the php script that same token.

Another solution without any sort of special bit, would be to pass along a timestamp with the request. Essentially, the client says "my latest bit of data arrived at 9:21:53 PM" The server then checks and responds "My most recent data was created before that, so you're still good. " or "I've got something that was created or modified on 9:22:53PM, you should download it."

With that method, you don't need to worry about who's who. Instead, you need to have modification/creation times on the data you're checking for staleness. You also need to make sure that the clients clock is synchronized correctly - better yet, don't trust the clients date and time information. Maybe utilize your new_data_check.php to provide the script with a timestamp it can pass along on subsequent requests.

0

精彩评论

暂无评论...
验证码 换一张
取 消