admin管理员组

文章数量:1431413

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 !

本文标签: How can we Restrict to access a certain wordpress page to all ip address except some which we allow