How to detect a returning visitor, and redirect to a specific URL? [closed]

社会主义新天地 提交于 2019-12-03 22:01:22
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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!