限制codeigniter管理面板的IP地址

时间:2011-03-01 18:27:00

标签: codeigniter

朋友们,我想在办公室限制我的codeigniter php网站后端的IP地址,仅出于安全原因。如果有人已经做过任何建议吗?

5 个答案:

答案 0 :(得分:7)

使用.htaccess的替代方法,您也可以限制PHP的访问(尽管.htaccess解决方案更强大):

$your_ip_address='123.123.123.123'; //change it to yours
if (!isset($_SERVER['REMOTE_ADDR']) || $_SERVER['REMOTE_ADDR'] != $your_ip_address) {
   exit();
}

在webroot文件夹中找到index.php,将上述代码添加到文件顶部。

希望这有帮助。

答案 1 :(得分:3)

如果您使用的是Apache,则可以使用.htaccess文件,例如..

order deny, allow
deny from all
allow from 111.222.333.444

在要保护的目录中创建文件.htaccess,并将其放在内部。

http://httpd.apache.org/docs/current/howto/htaccess.html

答案 2 :(得分:1)

我是通过在这个主题中跟随salah的帖子做到的:http://codeigniter.com/forums/viewthread/141775/

基本上,您创建一个名为admin /的文件夹,将主CI index.php复制到它,然后进行调整。

然后,将.htaccess添加到该文件夹​​。对于我的应用程序,我只需要AuthType,但IP允许/拒绝也应该正常工作。

当时我正在使用CI 1.7.2。

答案 3 :(得分:1)

简单的方法

$current_ip = $this->input->ip_address();
$your_ip_address='127.0.0.1';

if($current_ip == $your_ip_address){
    echo 'something is wrong';
    exit();
}

答案 4 :(得分:0)

如果您在CDN后面,并且想要使用允许多个IPS,我必须分享我的代码来做到这一点:

/**
 * return array value in key in case it exists and has value
 * @param array $arr
 * @param $key
 * @return bool|mixed
 */
function is($arr = array(), $key){
    if(isset($arr[$key]) && $arr[$key]){
        return $arr[$key];
    }
    return false;
}

/**
 * this function return the actual client IP in case it is behind CDN
 * @return string
 */
function getClientIp() {
    $ipAddress = '';
    if (is($_SERVER, 'HTTP_CLIENT_IP')) {
        $ipAddress = $_SERVER['HTTP_CLIENT_IP'];
    } else if (is($_SERVER, 'HTTP_X_FORWARDED_FOR')) {
        $ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else if (is($_SERVER, 'HTTP_X_FORWARDED')) {
        $ipAddress = $_SERVER['HTTP_X_FORWARDED'];
    } else if (is($_SERVER, 'HTTP_FORWARDED_FOR')) {
        $ipAddress = $_SERVER['HTTP_FORWARDED_FOR'];
    } else if (is($_SERVER, 'HTTP_FORWARDED')) {
        $ipAddress = $_SERVER['HTTP_FORWARDED'];
    } else if (is($_SERVER, 'REMOTE_ADDR')) {
        $ipAddress = $_SERVER['REMOTE_ADDR'];
    } else {
        $ipAddress = 'UNKNOWN';
    }

    return $ipAddress;
}

/**
 * Allowed IPS
 */
$allowedIPSArr = array(
    '127.0.0.1',
    '127.0.0.2',
);

if(!in_array(getClientIp(),$allowedIPSArr, true)){
// You might want to do some redirect here
    die();
}