How can we Restrict to access a certain wordpress page to all ip address except some which we allow

i am trying to restrict particular web page using php which should be displayed only to the allowed Ip Addresses and for

i am trying to restrict particular web page using php which should be displayed only to the allowed Ip Addresses and for remaining all IP Address it should not be accessible.

Requirement:

1.The page will be in sub menu list.

2.It should be displayed only to the allowed ip address only

Thank You in advance....

i am trying to restrict particular web page using php which should be displayed only to the allowed Ip Addresses and for remaining all IP Address it should not be accessible.

Requirement:

1.The page will be in sub menu list.

2.It should be displayed only to the allowed ip address only

Thank You in advance....

Share Improve this question edited Feb 28, 2017 at 12:53 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Feb 28, 2017 at 11:52 PraveenPraveen 2405 silver badges19 bronze badges 1
  • How are you trying that? Show us your code. – fuxia Commented Feb 28, 2017 at 12:54
Add a comment  | 

2 Answers 2

Reset to default 1

Haven't specifically done this yet but something similar

In the theme page/post template files you can get post id of current post/page by doing

$currentID = get_the_ID();

Then redirect all traffic not from the specified IPs

$ipArr = array('xx.xxx.xxx.xxx', 'xx.xxx.xxx.xxx');

 if (!in_array(@$_SERVER['REMOTE_ADDR'], $ipArr))
{
    header("Location: http://example/myhomepage");
    die();
}

You can block the access page for all IPs, but a whitelist.

function page_ip_restriction() {
    if(is_page('yourpage'))
    {
        // Whitelist
        $whitelist = array(
            '127.0.0.1',
            '127.0.0.2',
            '127.0.0.3',
        );

        // Check if current user IP is out of the whitelist, then redirect to home
        if(!in_array($_SERVER['REMOTE_ADDR'], $whitelist))
        {
            wp_redirect(home_url(), 403); // 403 -> Forbiden access
            exit();
        }
    }
}
add_action('init', 'page_ip_restriction', 10);

For your submenu, you have two ways, the wordpress one, or a css exclusion. But IMO, WordPress menus are really heavy to use.

If you want better IP address tracking, use a librairy.

Hope it's help you !

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745572268a4633750.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信