FILTER IP ADDRESSES WITH PHP > php

본문 바로가기
사이트 내 전체검색

php

FILTER IP ADDRESSES WITH PHP

페이지 정보

작성자 서방님 댓글 0건 조회 68회 작성일 14-09-02 10:17

본문

출처 : http://magp.ie/2011/09/01/filter-ip-addresses-with-php/


You may at some stage want to filter an online service based on IP address. In other words, you may want to block or grant access to a request based on their IP address. This can be handled in PHP by doing the following.

If you have the IP addresses, then it is trivial.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//First check IP address is valid
$request_ip = $_SERVER['REMOTE_ADDR'];
 
if ( !preg_match( "/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $request_ip ) )
    return false;
 
$blacklist = array(
'111.222.12.11',
'222.111.21.22',
'221.112.11.12'
);
 
//check that ip is not blacklisted
if ( in_array( $request_ip, $blacklist ) )
    return false;

If you want to include a range of IP addresses, best to use a regular expression.

1
2
3
4
5
6
7
8
9
$blacklist_ip_range = array(
    '/^122\.244\.(\d+)\.(\d+)/', //for IP address in the range 122.244.0.0 - 122.244.255.255
    '/^123\.(\d+)\.(\d+)\.(\d+)/', //for IP address in the range 123.0.0.0 - 123.255.255.255
);
 
foreach( $blacklist_ip_range as $ip ) {
    if( preg_match( $ip, $request_ip ) )
        return false;
    }

If you have a better solution, then please let me know.

c

댓글목록

등록된 댓글이 없습니다.

Total 612건 22 페이지
게시물 검색

회원로그인

접속자집계

오늘
246
어제
225
최대
1,347
전체
155,005
Latest Crypto Fear & Greed Index

그누보드5
Copyright © 서방님.kr All rights reserved.