Share Pastebin
Guest
Public paste!

AJAX-Based Auto-Updating Test Page

By: a guest | Apr 1st, 2010 | Syntax: PHP | Size: 5.92 KB | Hits: 285 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php
  2.     /**************************************
  3.      * AJAX-Based Auto-Updating Test Page *
  4.      *         By: Greg Schoppe           *
  5.      *         www.gschoppe.com           *
  6.      *            04/01/2010              *
  7.      * note:                              *
  8.      * Because of the use of the $_SERVER *
  9.      * array, this script may require     *
  10.      * APACHE to run properly.            *
  11.      **************************************/
  12.    
  13.     $myFileName = basename($_SERVER["SCRIPT_NAME"]);
  14.     $state = $_GET['state'];
  15.     //echo $state;
  16.     if($state == 'AJAX')
  17.     {
  18.         //---------------------------AJAX CONTENT---------------------------
  19.         $lastmtime = $_GET['mtime'];
  20.         $FName = $_GET['FName'];
  21.         $newmtime = filemtime($FName);
  22.         if($newmtime > $lastmtime)
  23.         {
  24.             echo $newmtime;
  25.         }
  26.         else
  27.         {
  28.             echo 'noChange';
  29.         }
  30.         exit;
  31.         //-------------------------END AJAX CONTENT-------------------------
  32.     }
  33.     else if($state == 'FORM')
  34.     {
  35.         //---------------------------FORM CONTENT---------------------------
  36.         ?>
  37.        
  38. <html>
  39.     <head>
  40.     <style>
  41.         body
  42.         {
  43.             text-align: center;
  44.             background-color: #000;
  45.             color: #fff;
  46.             margin: auto;
  47.         }
  48.         div
  49.         {
  50.             margin: auto;
  51.             height: 50px;
  52.             position: absolute;
  53.             top: 50%;
  54.             margin-top: -25px;
  55.             width: 100%;
  56.         }
  57.         form
  58.         {
  59.             margin: 0 auto;
  60.             text-align: center;
  61.             font-weight: bold;
  62.         }
  63.     </style>
  64.    
  65.     <script type="text/javascript">
  66.         function sendFName(thisform)
  67.         {
  68.               var FName = document.getElementById('FName');
  69.               parent.FName = FName.value;
  70.         }
  71.     </script>
  72.  
  73.     </head>
  74.     <body>
  75.         <div>
  76.             <form onsubmit="sendFName(this)">
  77.                 Address to Test:<br/>
  78.                 <input type="text" name="FName" id='FName' size="30"/>
  79.                 <input type="submit" value="Go"/>
  80.             </form>
  81.         </div>
  82.     </body>
  83. </html>
  84.        
  85.         <?php
  86.         //-------------------------END FORM CONTENT-------------------------
  87.     }
  88.     else
  89.     {
  90.         //-------------------------DEFAULT CONTENT--------------------------
  91.         ?>
  92. <html>
  93.     <head>
  94.         <title>AJAX Based Auto-Refreshing Test Page</title>
  95.         <script type="text/javascript">
  96.             var FName = "";
  97.             var mtime = 0;
  98.             function theOnLoad()
  99.             {
  100.                 contentFrame.location = '<?=$myFileName?>?state=FORM';
  101.                 checkFNVariable();
  102.             }
  103.             function makeRequest(theurl, thetype, thequery)
  104.             {
  105.                 var httpRequest;
  106.  
  107.                 if (window.XMLHttpRequest) // Mozilla, Safari, ...
  108.                 {
  109.                     httpRequest = new XMLHttpRequest();
  110.                     if (httpRequest.overrideMimeType)
  111.                     {
  112.                         httpRequest.overrideMimeType('text/xml');
  113.                         // See note below about this line
  114.                     }
  115.                 }
  116.                 else if (window.ActiveXObject) // IE
  117.                 {
  118.                     try
  119.                     {
  120.                         httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  121.                     }
  122.                     catch (e)
  123.                     {
  124.                         try
  125.                         {
  126.                             httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  127.                         }
  128.                         catch (e) {}
  129.                     }
  130.                 }
  131.  
  132.                 if (!httpRequest)
  133.                 {
  134.                     alert('Giving up :( Cannot create an XMLHTTP instance');
  135.                     return false;
  136.                 }
  137.                 httpRequest.onreadystatechange = function()
  138.                 {
  139.                     if (httpRequest.readyState == 4)
  140.                     {
  141.                         if (httpRequest.status == 200)
  142.                         {
  143.                             processHTTPRequest(httpRequest);
  144.                         } else {
  145.                             alert('There was a problem with the request.');
  146.                         }
  147.                     }
  148.                 };
  149.                 httpRequest.open(thetype, theurl+'?'+thequery, true);
  150.                 httpRequest.send(null);
  151.  
  152.             }
  153.  
  154.             function processHTTPRequest(httpRequest)
  155.             {
  156.                 //<--------------------------------HANDLE AJAX RESPONSE HERE!!!!
  157.                 if(httpRequest.responseText != 'noChange')
  158.                 {
  159.                     //alert(httpRequest.responseText);
  160.                     mtime = httpRequest.responseText;
  161.                     contentFrame.location.reload();
  162.                     contentFrame.location.href = FName;
  163.                 }
  164.             }
  165.  
  166.            
  167.            
  168.            
  169.            
  170.             function checkFNVariable()
  171.             {
  172.                 if (FName == "")
  173.                 {
  174.                     setTimeout("checkFNVariable()", 500);
  175.                 }
  176.                 else
  177.                 {
  178.                     contentFrame.location.href = FName;
  179.                     checkForChange();
  180.                 }
  181.             }
  182.            
  183.             function checkForChange()
  184.             {
  185.                 makeRequest('<?=$myFileName?>', 'POST', 'state=AJAX&FName='+FName+'&mtime='+mtime);
  186.                 setTimeout("checkForChange()", 3000);
  187.             }
  188.            
  189.         </script>
  190.     </head>
  191.     <frameset onLoad = 'theOnLoad()'>
  192.         <frame id='contentFrame' name='contentFrame'/>
  193.     </frameset>
  194. </html>
  195.         <?php
  196.         //-----------------------END DEFAULT CONTENT------------------------
  197.     }
  198. ?>