将首次访问的用户重定向到登陆页面 [英] Redirect first-time users to landing page

查看:65
本文介绍了将首次访问的用户重定向到登陆页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 example.com 上有一个 Wordpress 网站,在 example.com/landing-page 上有一个登录页面.着陆页有一个表格供用户填写以获得免费咨询.

I have a Wordpress website at example.com and I have a landing page at example.com/landing-page. The landing page has a form for users to fill out to get a free consultation.

有没有办法安装 cookie 或其他东西,以便当首次访问者访问 example.com 时,他们被重定向到 example.com/landing-page.但是当他们在登陆页面填写表单后,下次访问 example.com 时,他们会看到常规主页(不会被重定向).

Is there a way to install a cookie or something so that when first-time visitors visit example.com, they are redirected to example.com/landing-page. But after they fill out the form on the landing page, the next time they visit example.com, they will see the regular homepage (not be redirected).

推荐答案

您可以为此使用 javascript.确保包含 jQuery.

You could use javascript for this. Make sure that you have jQuery included.

将以下内容添加到包含在两个页面中的全局 javascript 文件中,因为我们将在主页和登录页面上使用这些辅助函数来检查 cookie 是否存在并创建它.

Add the following to your global javascript file that's included on both pages as we'll be using these helper functions on both the homepage and the landing page to check if the cookie exists and also to create it.

<script>
function create_cookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime( date.getTime() + (days*24*60*60*1000) );
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function get_cookie(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;
}
</script>

在您的主页上包含以下脚本,它基本上会检查 cookie 'form_submitted' 是否存在,如果不存在,则将用户重定向到登录页面.

Include the following script on your homepage which basically checks to see if the cookie 'form_submitted' exists, if not then redirect the user to the landing page.

<script>
// Redirect to landing page if 'form_submitted' cookie does not exist
if ( get_cookie( 'form_submitted' ) === 'undefined' ) {
    window.location.href = "http://xxx.com/landing-page";
}
</script>

在登陆页面上,您需要添加以下 javascript,它绑定一个监听事件以提交表单,当它发生时它会创建 cookie,因此下次用户访问主页时,他们赢了不会被重定向.

And on the landing page, you'll need to add the following javascript which binds a listening event for submit of the form and when it happens it creates the cookie and therefore the next time the user goes to the homepage, they won't be redirected.

<script>
jQuery(document).ready(function($) {

    // Listen for the form submit event
    $('#form-id').on('submit', function(){

        // Create cookie so that the user is no longer redirected
        create_cookie('form_submitted', 'true', 30);

    });

});
</script>

我希望这会有所帮助.祝你好运!=)

I hope this helps. Good luck! =)

我刚刚阅读了在我写答案时留下的评论.只是想补充一点,我添加的 create_cookie 函数允许您在希望 cookie 过期之前传入天数,在我的示例中为 30,这样您就可以将其设置得相当高以使其持续更长时间.

I've just read comments that were left on your original post whilst I was writing my answer. Just wanted to add that the create_cookie function I've added allows you to pass in the number of days, 30 in my example, before you want the cookie to expire so you could set that quite high to make it last longer.

显然,人们清除浏览器 cookie 的 JavaScript cookie 总是存在风险,但没有其他方法可以在未登录的用户上跟踪此类内容.对于无法删除的用户而言,还有什么更安全的方法是存储在服务器上的 PHP 会话,但这比这要困难得多,甚至这也不是防弹的,因为您用来将用户与会话相关联的用户数据可以像他们的 IP、浏览器等一样改变,所以总是有一个风险.

Obviously there's always the risk with javascript cookies that people clear their browser cookies but there's no other way to track something like this on users that are not logged in. What could be more secure in terms of users not be able to remove it is PHP sessions stored on the server but that's going to be a lot more difficult than this and even that's not bullet proof as user data that you use to associate the user to a session with can change like their IP, browser etc so there's always a risk.

您可以在表单页面上看到一个链接,上面写着我已经完成了这个表单,请带我到主网站",它会再次设置 cookie.

What you could have on the form page is a link saying "I've already completed this form, take me to the main website" which sets the cookie again.

最后,您会注意到我的解决方案是轻量级的,不包含不必要的插件,如 $.cookie,因为我在很大程度上使用了 javascript,仅使用 jQuery 来准备文档并绑定提交事件.

Lastly, you'll notice that my solution is lightweight and includes no unnecessary plugins like $.cookie as I've used javascript in large part, only using jQuery for document ready and binding the submit event.

这篇关于将首次访问的用户重定向到登陆页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆