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');
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统