If all you want to do is check a cookie, you are better off doing so in the browser. While cookies are stored as a property of document, namely document.cookie, they are in point of fact not local to document, or even to tab/window for that matter. They are - session local storage.
Thus, if you set a cookie which expires after 10 seconds in tab 1, and wait 10+ seconds before checking the cookie, you'll find no such cookie (it expired). However, if you now load tab2 which once again sets the same cookie name with a 10 second duration and go back to check the cookie in tab 1 - it will be there for 10 seconds.
And that's the way you can implement Session Storage on browsers that doens't support it, just like you can use window.name to implement Local Storage.
Not sure why an extra space made its way into the cookie name, but rather than finding out why, I used trim(). And since trim() doesn't exist in IE8, I added it to String.prototype if needed. The rest is straightforward cookie handling.
<?php
header('content-type: text/html; charset=utf-8');
setcookie('check', 'whatever', time() + 10);
?>
<!DOCTYPE html>
<html>
<head>
<title>Cookie Checker-o 4000!</title>
<script>
if (String.prototype.trim == undefined) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
}
function cookietimer(cname) {
var carr = document.cookie.split(';');
var found = false;
for (var i = carr.length - 1; i < carr.length; ++i) {
var pair = carr[i].split('=');
pair[0] = pair[0].trim();
cname = cname.trim();
if (pair[0].localeCompare(cname) == 0) {
found = true;
break;
}
}
if (found)
{
alert(pair[0] + ': ' + pair[1]);
}
else {
alert('no such cookie');
}
}
</script>
</head>
<body>
<div>
<input type="button" onclick="cookietimer('check');" value="Cookie Timer Check">
</div>
</body>
</html>
Edit: The point of the code is to show what I described above. Open a tab and request the above page. Click button. Wait 10 seconds, click button: "no such cookie". Open a second tab, request above page. Go to first tab, click button again. Wait 10 seconds...