[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Update page every minute

Hi Experts,

I have a page called online.php which contains some code I want to run every minute, when the user is online. I have attached the code from online.php.

How can I via AJAX/ or Query make this page reload every minute?

Thanks in advance!
include_once($_SERVER["DOCUMENT_ROOT"] . "/functions/session_start.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/functions/generel.php");

// Check if the user is online
if (isset($_SESSION['MM_UserId'])) {
$sql="UPDATE users SET online_status=NOW() WHERE id = '".$_SESSION['MM_UserId']."'";

Open in new window

  • 2
  • 2
1 Solution
Put this in head section :
<script language="javascript" type="text/javascript">
setTimeout(function() {
   var xmlhttp;
   if(window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   xmlhttp.onreadystatechange = function() {}
   xmlhttp.open("GET","yourpage.php", true);
}, 60000); // 60 x 1000ms = 1min

Open in new window

change yourpage.php with your page filename.
An old trick to refresh pages on a timer is to use META tags. What I think you're trying to do, however, is to monitor a user in the background to see if they're still online or not.

I would suggest setting up an Iframe on your page. This could be hidden (so as to genuinely be in the background), or could be included with advertising or some other object.

Create a page that shows the aforementioned advertising if desired, or that simply calls the PHP code if you prefer an invisible arrangement. In the HTML output on that page, place this in the <head> section:

<meta http-equiv="refresh" content="[..x..]" url="[..y..]" />

You will want to replace [..x..] with the number of seconds between refreshes (in your case, 60), and [..y..] with the URL of that page.

The effect will be that the web page inside the Iframe (and the PHP that is called upon each request) is executed every 60 seconds, without disturbing the rest of what your user is looking at.

If you are not familiar with Iframes and need help with this, I am happy to assist.

Looking at your solution, I see references to ActiveX. I have not done much work with ActiveX in the last few years, but from what I remember it works great in Internet Explorer but not at all in any other browser. Is that still the case?
>I see references to ActiveX.

Yes only if XMLhttpRequest is not available.
Check this page if you want more info : http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now