Update page every minute

Posted on 2011-10-21
Last Modified: 2012-05-12
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

Question by:sittinDuck
    LVL 81

    Expert Comment

    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() {}"GET","yourpage.php", true);
    }, 60000); // 60 x 1000ms = 1min

    Open in new window

    change yourpage.php with your page filename.
    LVL 1

    Accepted Solution

    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.
    LVL 1

    Expert Comment


    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?
    LVL 81

    Expert Comment

    >I see references to ActiveX.

    Yes only if XMLhttpRequest is not available.
    Check this page if you want more info :

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Hello World !, Thanks : My Sincere thanks to @Michael Male who encouraged me to write an article on this in my  blog ( Introduction:       There are several technical issues a developer may face in his…
    Today I am discussing AJAX problems in IE7 and I bet this will helps many guys out here who have problems with AJAX work. Lets start with the discovery of problem and then we will talk about its different solutions. My last two projects included …
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now