It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened,
visit the help center.
Closed 7 years ago.
I am developing a new website that needs the ability to detect if the user has visited the website previously, then direct them to a specific URL (i.e. example.com/welcomeback.html) if they have.
I assume that this needs to be done with the usage of cookies and javascript, but I can't find any tutorials on this anywhere.
The help is much appreciated. Thank you.
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
gets cookie
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
if(getCookie('visited'))
{
location.href="redirecturl";
}else
{
setCookie('visited',1,365);
}
tutorial for cookies
Check if cookie is present and do whatever you want (i.e. redirect), if not found -set cookie with expiration so it is not session-cookie and continue.
Side note: getting "magic redirect for return users" working in user-friendly manner is hard, consider inline welcome message instead.
Another approach: if you can authenticate users through some other means (i.e. some form of OAuth, or Windows authentication) than you can instead check if user is know by saving information to server side database and not relying on cookies at all.