Skip to content

Instantly share code, notes, and snippets.

@betweenbrain
Created June 29, 2012 21:00
Show Gist options
  • Save betweenbrain/3020609 to your computer and use it in GitHub Desktop.
Save betweenbrain/3020609 to your computer and use it in GitHub Desktop.
Check for cookie support, then check for secret variable in URL to permit access to site, otherwise redirect
// from http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
// from http://www.quirksmode.org/js/cookies.html
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
// http://sveinbjorn.org/cookiecheck
function are_cookies_enabled()
{
var cookieEnabled = (navigator.cookieEnabled) ? true : false;
if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
{
document.cookie="testcookie";
cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
}
return (cookieEnabled);
}
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
if(are_cookies_enabled()) {
if(getUrlVars() == 'permitted') {
createCookie('permitted','1',365);
}
if(!readCookie('permitted')) {
window.location = "http://betweenbrain.com/"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment