Extralight browser-webserver communication via cookies (+) [message #172662] |
Thu, 24 February 2011 03:22 |
n00m
Messages: 25 Registered: February 2011
Karma:
|
Junior Member |
|
|
It could replace 97% of all xmlhttprequest stuff. Tested in IE and FF.
Below just a minimalistic example of the whole idea.
====================================================
<?php // me.php
if (array_key_exists('sel', $_COOKIE) && $_COOKIE['sel']) {
setcookie('sel', 'You_chose_'.$_COOKIE['sel'].'!', 0);
header('HTTP/1.1 204'); // <- note this...
exit(); // and this. The me.php is loaded only *ONCE*;
}
?>
<html>
<head>
<script>
var dT;
var prev_cookie;
function receiver() {
if (document.cookie == prev_cookie)
return;
clearInterval(dT);
document.myform.txtArea.value = unescape(document.cookie);
}
function sender() {
document.cookie = "sel=" + document.getElementById("sel").value;
prev_cookie = document.cookie;
window.location.href = "me.php";
dT = setInterval(receiver, 100);
}
function reset_cookie() {
document.cookie = "sel=";
}
</script>
</head>
<body>
<br><br>
<center>
<form name="myform">
<label><?php echo date(DATE_RFC822); ?></label>
<br><br>
<textarea name="txtArea" cols="30" rows="8"></textarea>
<br><br>
<select id="sel" style="width:200px;" onchange="sender();">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br><br>
<input type="button" value="reset_cookie();"
onclick="reset_cookie();" />
</form>
</center>
</body>
</html>
=====================================================
|
|
|