문제

PHP 페이지가 있습니다. 방화벽 내부의 클라이언트에 대한 실행 액세스를 제한해야합니다.

클라이언트 IP-Address를 찾아 IP 범위 (예 : 10과 일치시킬 수있는 PHP 스크립트를 어떻게 작성합니까?..* 또는 200.10.10.*).

도움이 되었습니까?

해결책

당신이 사용할 수있는 ip2long 점선 쿼드를 긴 값으로 변환하려면 산술을 수행하여 주어진 네트워크/마스크 조합을 확인하십시오.

$network=ip2long("200.10.10.0");
$mask=ip2long("255.255.255.0");

$remote=ip2long($_SERVER['REMOTE_ADDR']);

if (($remote & $mask) == $network)
{
   //match!
}
else
{
   //does not match!
}

다른 팁

Apache를 사용한다고 가정하면 사용할 수있는 Mod_authz_host라는 모듈이 있습니다.

파일 지시문과 함께 다양한 IP 주소에 대한 주어진 PHP 스크립트에 대한 액세스를 제한 할 수 있습니다.

다음은 해당 모듈의 설명서에 대한 링크입니다.http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html

다음은 빠른 예입니다 (PHP 파일이 admin.php라고 가정) :

<file admin.php>
  Order Deny,Allow
  Deny from all
  Allow from 200.10.10
</file>

여기에 제안 된 다른 솔루션에 추가 된 이점은 응용 프로그램 논리 외부에서 보안 측면을 제어 할 수 있다는 것입니다.

Spiders/Bots/Clients 등에 대한 PHP Limit/Block 웹 사이트 요청. 여기에서는 웹 사이트 트래픽을 줄이기위한 원치 않는 요청을 차단할 수있는 PHP 기능을 작성했습니다. 거미, 봇 및 성가신 고객을위한 신. 데모:http://szczepan.info/9-webdesign/php/1-php-limit-limit-website-requests-for-piders-clients-etc.html

/* Function which can Block unwanted Requests
 * @return boolean/array status
 */
function requestBlocker()
{
        /*
        Version 1.0 11 Jan 2013
        Author: Szczepan K
        http://www.szczepan.info
        me[@] szczepan [dot] info
        ###Description###
        A PHP function which can Block unwanted Requests to reduce your Website-Traffic.
        God for Spiders, Bots and annoying Clients.

        */

        $dir = 'requestBlocker/'; ## Create & set directory writeable!!!!

        $rules   = array(
                #You can add multiple Rules in a array like this one here
                #Notice that large "sec definitions" (like 60*60*60) will blow up your client File
                array(
                        //if >5 requests in 5 Seconds then Block client 15 Seconds
                        'requests' => 5, //5 requests
                        'sek' => 5, //5 requests in 5 Seconds
                        'blockTime' => 15 // Block client 15 Seconds
                ),
                array(
                        //if >10 requests in 30 Seconds then Block client 20 Seconds
                        'requests' => 10, //10 requests
                        'sek' => 30, //10 requests in 30 Seconds
                        'blockTime' => 20 // Block client 20 Seconds
                ),
                array(
                        //if >200 requests in 1 Hour then Block client 10 Minutes
                        'requests' => 200, //200 requests
                        'sek' => 60 * 60, //200 requests in 1 Hour
                        'blockTime' => 60 * 10 // Block client 10 Minutes
                )
        );
        $time    = time();
        $blockIt = array();
        $user    = array();

        #Set Unique Name for each Client-File 
        $user[] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'IP_unknown';
        $user[] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
        $user[] = strtolower(gethostbyaddr($user[0]));

        # Notice that i use files because bots does not accept Sessions
        $botFile = $dir . substr($user[0], 0, 8) . '_' . substr(md5(join('', $user)), 0, 5) . '.txt';


        if (file_exists($botFile)) {
                $file   = file_get_contents($botFile);
                $client = unserialize($file);

        } else {
                $client                = array();
                $client['time'][$time] = 0;
        }

        # Set/Unset Blocktime for blocked Clients
        if (isset($client['block'])) {
                foreach ($client['block'] as $ruleNr => $timestampPast) {
                        $left = $time - $timestampPast;
                        if (($left) > $rules[$ruleNr]['blockTime']) {
                                unset($client['block'][$ruleNr]);
                                continue;
                        }
                        $blockIt[] = 'Block active for Rule: ' . $ruleNr . ' - unlock in ' . ($left - $rules[$ruleNr]['blockTime']) . ' Sec.';
                }
                if (!empty($blockIt)) {
                        return $blockIt;
                }
        }

        # log/count each access
        if (!isset($client['time'][$time])) {
                $client['time'][$time] = 1;
        } else {
                $client['time'][$time]++;

        }

        #check the Rules for Client
        $min = array(
                0
        );
        foreach ($rules as $ruleNr => $v) {
                $i            = 0;
                $tr           = false;
                $sum[$ruleNr] = '';
                $requests     = $v['requests'];
                $sek          = $v['sek'];
                foreach ($client['time'] as $timestampPast => $count) {
                        if (($time - $timestampPast) < $sek) {
                                $sum[$ruleNr] += $count;
                                if ($tr == false) {
                                        #register non-use Timestamps for File 
                                        $min[] = $i;
                                        unset($min[0]);
                                        $tr = true;
                                }
                        }
                        $i++;
                }

                if ($sum[$ruleNr] > $requests) {
                        $blockIt[]                = 'Limit : ' . $ruleNr . '=' . $requests . ' requests in ' . $sek . ' seconds!';
                        $client['block'][$ruleNr] = $time;
                }
        }
        $min = min($min) - 1;
        #drop non-use Timestamps in File 
        foreach ($client['time'] as $k => $v) {
                if (!($min <= $i)) {
                        unset($client['time'][$k]);
                }
        }
        $file = file_put_contents($botFile, serialize($client));


        return $blockIt;

}


if ($t = requestBlocker()) {
        echo 'dont pass here!';
        print_R($t);
} else {
        echo "go on!";
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top