<?php
/**************************************
* AJAX-Based Auto-Updating Test Page *
* By: Greg Schoppe *
* www.gschoppe.com *
* 04/01/2010 *
* note: *
* Because of the use of the $_SERVER *
* array, this script may require *
* APACHE to run properly. *
**************************************/
$myFileName = basename($_SERVER["SCRIPT_NAME"]);
$state = $_GET['state'];
//echo $state;
if($state == 'AJAX')
{
//---------------------------AJAX CONTENT---------------------------
$lastmtime = $_GET['mtime'];
$FName = $_GET['FName'];
$newmtime = filemtime($FName);
if($newmtime > $lastmtime)
{
echo $newmtime;
}
else
{
echo 'noChange';
}
exit;
//-------------------------END AJAX CONTENT-------------------------
}
else if($state == 'FORM')
{
//---------------------------FORM CONTENT---------------------------
?>
<html>
<head>
<style>
body
{
text-align: center;
background-color: #000;
color: #fff;
margin: auto;
}
div
{
margin: auto;
height: 50px;
position: absolute;
top: 50%;
margin-top: -25px;
width: 100%;
}
form
{
margin: 0 auto;
text-align: center;
font-weight: bold;
}
</style>
<script type="text/javascript">
function sendFName(thisform)
{
var FName = document.getElementById('FName');
parent.FName = FName.value;
}
</script>
</head>
<body>
<div>
<form onsubmit="sendFName(this)">
Address to Test:<br/>
<input type="text" name="FName" id='FName' size="30"/>
<input type="submit" value="Go"/>
</form>
</div>
</body>
</html>
<?php
//-------------------------END FORM CONTENT-------------------------
}
else
{
//-------------------------DEFAULT CONTENT--------------------------
?>
<html>
<head>
<title>AJAX Based Auto-Refreshing Test Page</title>
<script type="text/javascript">
var FName = "";
var mtime = 0;
function theOnLoad()
{
contentFrame.location = '<?=$myFileName?>?state=FORM';
checkFNVariable();
}
function makeRequest(theurl, thetype, thequery)
{
var httpRequest;
if (window.XMLHttpRequest) // Mozilla, Safari, ...
{
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType)
{
httpRequest.overrideMimeType('text/xml');
// See note below about this line
}
}
else if (window.ActiveXObject) // IE
{
try
{
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest)
{
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = function()
{
if (httpRequest.readyState == 4)
{
if (httpRequest.status == 200)
{
processHTTPRequest(httpRequest);
} else {
alert('There was a problem with the request.');
}
}
};
httpRequest.open(thetype, theurl+'?'+thequery, true);
httpRequest.send(null);
}
function processHTTPRequest(httpRequest)
{
//<--------------------------------HANDLE AJAX RESPONSE HERE!!!!
if(httpRequest.responseText != 'noChange')
{
//alert(httpRequest.responseText);
mtime = httpRequest.responseText;
contentFrame.location.reload();
contentFrame.location.href = FName;
}
}
function checkFNVariable()
{
if (FName == "")
{
setTimeout("checkFNVariable()", 500);
}
else
{
contentFrame.location.href = FName;
checkForChange();
}
}
function checkForChange()
{
makeRequest('<?=$myFileName?>', 'POST', 'state=AJAX&FName='+FName+'&mtime='+mtime);
setTimeout("checkForChange()", 3000);
}
</script>
</head>
<frameset onLoad = 'theOnLoad()'>
<frame id='contentFrame' name='contentFrame'/>
</frameset>
</html>
<?php
//-----------------------END DEFAULT CONTENT------------------------
}
?>