Hi i need to redirect users using javascript and cookies.
The desired script need to work in this way
Any page on the site (including pages on folders) check for a cookie.
If the cookie isn’t there the script redirect to the home page of the site.
This need to happen once.
Basically the cookie will be set by the home page, so all pages will check if the users had been there or not. if not, the user will get redirected, if yes the script do nothing.
The script need to work in a subdomain – the index is in a subdomain (example.domain.com) so all pages like
example.subdomain.com/folder/
example.subdomain.com/folder/folder2/
example.subdomain.com/folder/folder 2/page.html
All these types of pages redirect to example.subdomain.com
So far i get something like this
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + “=”);
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(“;”,c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return “”;
}
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ “=” +escape(value)+
((expiredays==null) ? “” : “;expires=”+exdate.toUTCString());
}
function checkCookie()
{
username=getCookie(‘username’);
if (username!=null && username!=””)
{
}
else
{
window.location.replace = “
if (username!=null && username!=””)
{
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ “=” +escape(value)+
((expiredays==null) ? “” : “;expires=”+exdate.toUTCString());
}
}
}
}
But that don’t work
If you have any idea how to achieve that please let me know.
Sorry for my poor english