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

感情迁移 提交于 2019-12-05 08:16:26

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/13655941/how-to-detect-a-returning-visitor-and-redirect-to-a-specific-url

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