dtpoius74857 2019-02-14 01:53
浏览 351

如果用户浏览器中存在cookie,如何将用户重定向到外部URL

I would like someone to help with how to redirect a returning user who has a cookie set in his/her browser to http://google.co.uk. I'm working on a Age Verification System in which everything is working as it should in wordpress but the only issue i've been trying to figure out is how to redirect users who are underage to google.co.uk until the cookie expires.

This is the process, when a users visits the website, they are prompted to fill in their D.O.B, if within the allowed age (18+), they are welcomed to the website and can continue browsing the website but if underaged, i used Ajax and PHP (PHP to sets the cookies name VAage which has a content of under 18 as seen in the screenshot below) while ajax handles the user's experience/ interaction with the form without having to reload page.

Age Verification

Now the part that hasn't been cooperating is that... when a user is under 18 and has been redirected to http://google.co.uk (which to this point is working fine) and decides to revisit the website immediately or revisit the website while the cookies hasn't expired or being deleted from his/her browser should be redirected back to google.co.uk again and again (preventing them from access the website).

I have tried adding this code to function.php file, header.php, index.php of the theme but doesn't work;

function is_user_with_VAage_cookies () {
if ( isset( $_COOKIE['VAage'] ) ) {
        header('Location: https://google.co.uk'); 
        exit;
    }
}

i as well tried using JavaScript instead of using PHP's header() function, like this,

function is_user_with_VAage_cookies () {
if ( isset( $_COOKIE['VAage'] ) ) {
        ?>
            <script type="text/javascript">var win = window.open('https://google.co.uk', '_self');</script>
    <?php

    }
}

i'm not sure what i'm doing wrong or if there's a hook to make this work. I have tried hooking it like this add_action('init', 'is_user_with_VAage_cookies'); (as seen here) but each time i test the Age verification and try coming back to the website after being redirected away for being under 18, it still loads up the website which i don't want. i want underaged users to be redirected to google immediately they try accessing the website again till the cookie expires (which is set to expire 7days).

Thanks in advance for your help

  • 写回答

1条回答 默认 最新

  • doqrjrc95405 2019-02-14 12:22
    关注

    Finally got it to work, the issue was with the path ... as seen in the image above the part was pointing to /cbd/wp-admin (in which it should point to /cbd/) ... which is the root path (C://xampp/htdocs/cbd/). After setting the path like this setcookie($cookie_name, $true, time()+3600, '/');

    I used this php code to redirect user if cookie exist

    function is_user_with_VAage_cookies () {
        if(isset($_COOKIE['VAage'])) { //Which return this array [VAage]=>user18 if you print_r($_COOKIE);
            ?>
            <script type="text/javascript">
                window.open('https://google.co.uk', '_self');
            </script>
            <?php
        }
    }
    add_action('init', 'is_user_with_VAage_cookies');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题