PHP - refresh for "live data"

  • dyfrin
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 10, 2006
  • Posts: 351
  • Loc: WI
  • Status: Offline

Post March 13th, 2008, 11:40 am

Hey guys,
Didn't see this yet on ozzu, what would you say is the best method to refresh php generated output (echo/print) like ventrilo server status for instance?
Want to avoid the annoying page click noise I see some places, ajax should help but not finding anything relevant.

Any example would help.
dyfrin.com
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post March 13th, 2008, 11:40 am

  • egranville
  • Beginner
  • Beginner
  • User avatar
  • Joined: Oct 29, 2004
  • Posts: 44
  • Loc: The Netherlands
  • Status: Offline

Post March 13th, 2008, 12:13 pm

What Are you trying to accomplish exactly?

Data refresh from a continuing PHP process?
  • dyfrin
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 10, 2006
  • Posts: 351
  • Loc: WI
  • Status: Offline

Post March 13th, 2008, 12:32 pm

For Ventrilo you have a display.php page, an exe(win)/script(nix) that collects the data from the ventsrv, and an includes file that gets the data from the script and assigns the php variables to the data so your display page can output in a format you like.

The display is just echoing variables output by the script. The script is ran from the includes file as such:
PHP Code: [ Download ] [ Select ]
function Request()
   {
      $cmdline = $this->m_cmdprog;
      $cmdline .= " -c" . $this->m_cmdcode;
      $cmdline .= " -t" . $this->m_cmdhost;
     
      if ( strlen( $this->m_cmdport ) )
      {
         $cmdline .= ":" . $this->m_cmdport;
         
         // For password to work you MUST provide a port number.
         
         if ( strlen( $this->m_cmdpass ) )
            $cmdline .= ":" . $this->m_cmdpass;
      }
     
      // Just in case.
     
      $cmdline = escapeshellcmd( $cmdline );
     
      // Execute the external command.
     
      $pipe = popen( $cmdline, "r" );
      if ( $pipe === false )
      {
         $this->m_error = "PHP Unable to spawn shell.";
         return -10;
      }
     
      // Process the results coming back from the shell.
     
      $cnt = 0;
     
      while( !feof( $pipe ) )
      {
         $s = fgets( $pipe, 1024 );
 
         if ( strlen( $s ) == 0 )
            continue;
           
         $rc = $this->Parse( $s );
         if ( $rc < 0 )
         {
            pclose( $pipe );
            return( $rc );
         }
         
         $cnt += 1;
      }
     
      pclose( $pipe );
     
      if ( $cnt == 0 )
      {
         // This is possible since the shell might not be able to find
         // the specified process but the shell did spawn. More likely to
         // occur then the -10 above.
         
         $this->m_error = "PHP Unable to start external status process.";
         return -11;
      }
     
      return 0;
  1. function Request()
  2.    {
  3.       $cmdline = $this->m_cmdprog;
  4.       $cmdline .= " -c" . $this->m_cmdcode;
  5.       $cmdline .= " -t" . $this->m_cmdhost;
  6.      
  7.       if ( strlen( $this->m_cmdport ) )
  8.       {
  9.          $cmdline .= ":" . $this->m_cmdport;
  10.          
  11.          // For password to work you MUST provide a port number.
  12.          
  13.          if ( strlen( $this->m_cmdpass ) )
  14.             $cmdline .= ":" . $this->m_cmdpass;
  15.       }
  16.      
  17.       // Just in case.
  18.      
  19.       $cmdline = escapeshellcmd( $cmdline );
  20.      
  21.       // Execute the external command.
  22.      
  23.       $pipe = popen( $cmdline, "r" );
  24.       if ( $pipe === false )
  25.       {
  26.          $this->m_error = "PHP Unable to spawn shell.";
  27.          return -10;
  28.       }
  29.      
  30.       // Process the results coming back from the shell.
  31.      
  32.       $cnt = 0;
  33.      
  34.       while( !feof( $pipe ) )
  35.       {
  36.          $s = fgets( $pipe, 1024 );
  37.  
  38.          if ( strlen( $s ) == 0 )
  39.             continue;
  40.            
  41.          $rc = $this->Parse( $s );
  42.          if ( $rc < 0 )
  43.          {
  44.             pclose( $pipe );
  45.             return( $rc );
  46.          }
  47.          
  48.          $cnt += 1;
  49.       }
  50.      
  51.       pclose( $pipe );
  52.      
  53.       if ( $cnt == 0 )
  54.       {
  55.          // This is possible since the shell might not be able to find
  56.          // the specified process but the shell did spawn. More likely to
  57.          // occur then the -10 above.
  58.          
  59.          $this->m_error = "PHP Unable to start external status process.";
  60.          return -11;
  61.       }
  62.      
  63.       return 0;


The goal is to have a live view of what happens on the server. This can be done by constantly refreshing the page, so just refreshing the display.php should work.
dyfrin.com
  • dyfrin
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 10, 2006
  • Posts: 351
  • Loc: WI
  • Status: Offline

Post March 13th, 2008, 7:07 pm

got it
Code: [ Download ] [ Select ]
function ajax(url,target)
{
    // native XMLHttpRequest object
// document.getElementById(target).innerHTML = '<dl><dt></dt><dd></dd></dl>';
if (window.XMLHttpRequest) {
     req = new XMLHttpRequest();
     req.onreadystatechange = function() {ajaxDone(target);};
     req.open("GET", url, true);
     req.send(null);
// IE/Windows ActiveX version
} else if (window.ActiveXObject) {
     req = new ActiveXObject("Microsoft.XMLHTTP");
     if (req) {
         req.onreadystatechange = function() {ajaxDone(target);};
         req.open("GET", url, true);
         req.send();
     }
}
         setTimeout("ajax(page,'ventdisplay')", 3000);
}

function ajaxDone(target) {
// only if req is "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200 || req.status == 304) {
results = req.responseText;
document.getElementById(target).innerHTML = results;
} else {
document.getElementById(target).innerHTML="ajax error:\n" +
req.statusText;
}
}
}
  1. function ajax(url,target)
  2. {
  3.     // native XMLHttpRequest object
  4. // document.getElementById(target).innerHTML = '<dl><dt></dt><dd></dd></dl>';
  5. if (window.XMLHttpRequest) {
  6.      req = new XMLHttpRequest();
  7.      req.onreadystatechange = function() {ajaxDone(target);};
  8.      req.open("GET", url, true);
  9.      req.send(null);
  10. // IE/Windows ActiveX version
  11. } else if (window.ActiveXObject) {
  12.      req = new ActiveXObject("Microsoft.XMLHTTP");
  13.      if (req) {
  14.          req.onreadystatechange = function() {ajaxDone(target);};
  15.          req.open("GET", url, true);
  16.          req.send();
  17.      }
  18. }
  19.          setTimeout("ajax(page,'ventdisplay')", 3000);
  20. }
  21. function ajaxDone(target) {
  22. // only if req is "loaded"
  23. if (req.readyState == 4) {
  24. // only if "OK"
  25. if (req.status == 200 || req.status == 304) {
  26. results = req.responseText;
  27. document.getElementById(target).innerHTML = results;
  28. } else {
  29. document.getElementById(target).innerHTML="ajax error:\n" +
  30. req.statusText;
  31. }
  32. }
  33. }


<body onload="ajax(page,'ventdisplay')">

<div id="ventdisplay"></div>
dyfrin.com
  • dyfrin
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 10, 2006
  • Posts: 351
  • Loc: WI
  • Status: Offline

Post March 13th, 2008, 9:39 pm

ok once again IE a pain!

Won't work in IE even with active x there.
hmm going to mess with it.
dyfrin.com
  • joebert
  • Weathered
  • Genius
  • User avatar
  • Joined: Feb 10, 2004
  • Posts: 11884
  • Loc: Clearwater, FL
  • Status: Offline

Post March 14th, 2008, 12:12 am

http://jquery.com/

With the library loaded, you'ld do somthing like this to refresh it every 5 seconds.
Code: [ Download ] [ Select ]
var vfresh = function()
{
   $('#ventdisplay').empty();
   $('#ventdisplay').load('vstatus.php');
}
setInterval(vfresh, 5000);
  1. var vfresh = function()
  2. {
  3.    $('#ventdisplay').empty();
  4.    $('#ventdisplay').load('vstatus.php');
  5. }
  6. setInterval(vfresh, 5000);
Why yes, yes I am.
  • dyfrin
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 10, 2006
  • Posts: 351
  • Loc: WI
  • Status: Offline

Post March 14th, 2008, 8:32 am

Thank Joebert, knowing what to invest your time in to do it right is a lot better than searching/modifying scripts!

I wasn't sure what the use of the empty was really for. the <span id="ventdisplay"></span> is empty, so dropped that comment.

Also the IE problem came back until I thought a minute about php being cached, added the header no cache and works on both, with a LOT less code on the page, but a little bigger total download with the jquery. Will be adding a few more nice things to utilize it though.
dyfrin.com
  • dyfrin
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 10, 2006
  • Posts: 351
  • Loc: WI
  • Status: Offline

Post July 2nd, 2008, 8:43 am

Hey Joebert (or anyone), have you seen issues with a 1 second [1000] refresh on javascript causing the browser to become unresponsive if left open a minute or so?

At 2 second refresh it doesn't lock up the browser, but at 1 it will.
dyfrin.com
  • joebert
  • Weathered
  • Genius
  • User avatar
  • Joined: Feb 10, 2004
  • Posts: 11884
  • Loc: Clearwater, FL
  • Status: Offline

Post July 2nd, 2008, 11:12 am

Can't say I have. :scratchhead:
Just out of curiosity, what happens at 1001 milliseconds ?
Why yes, yes I am.
  • dyfrin
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 10, 2006
  • Posts: 351
  • Loc: WI
  • Status: Offline

Post July 4th, 2008, 1:00 pm

After testing more, it was only happening on IE. FF and Safari didn't lock up.
Changed it to 1001 and didn't see difference.

Changed it to 1050 and it seemed to make a difference, the browser kept responsiveness.
dyfrin.com
  • joebert
  • Weathered
  • Genius
  • User avatar
  • Joined: Feb 10, 2004
  • Posts: 11884
  • Loc: Clearwater, FL
  • Status: Offline

Post July 4th, 2008, 1:07 pm

Would going with 2 seconds hurt the application ?

On the bright side it would cut bandwidth usage for the application in half. :D
Why yes, yes I am.

Post Information

  • Total Posts in this topic: 11 posts
  • Users browsing this forum: No registered users and 344 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.