Teralios
Moderator
Perfekter User
Dabei seit: 18.09.2005
Herkunft: Berlin
Posts: 2542
|
Diese Funktion überprüft, ob eine gegebene IP dem IPv4 oder IPv6 Format entspricht.
PHP |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
|
<?php
/**
* Replace short-zero-block in ipv6.
*
* Example: ::1 => 0:0:0:0:0:0:0:1
* @param string $ip
* @return boolean
*/
function fillZerosInIP($ip) {
// strpos is little buggy, do nit find :: when local ip or ::1
if (substr($ip, 0, 2) == '::') $ip = 0 . $ip;
// search ::
if (strpos($ip, '::')) {
// add 0 to ip, when is type: 0424:4224::
if (substr($ip, -2, 2) == '::') $ip .= '0';
// explode blocks.
$ipBlocks = explode(':', $ip);
$count = 8 - count($ipBlocks);
$ip = '';
// test all blocks and create new ip string.
foreach ($ipBlocks AS $ipBlock) {
if ($ip != '') $ip .= ':';
// if zero block, insert blocks with 0.
if ($ipBlock == '') {
for ($i = 0; $i <= $count; $i++) {
if ($ip != '' && $i != 0) $ip .= ':';
$ip .= '0';
}
}
else {
$ip .= $ipBlock;
}
}
}
return $ip;
}
/**
* Check given ip for ipv6.
* @param string $ip
* @return boolean
*/
function isIPv6($ip) {
return (preg_match('#^[0-9A-F]{0,4}(:([0-9A-F]{0,4})){0,7}$#s', $ip)) ? true : false;
}
/**
* Check given ip for ipv4.
* @param string $ip
* @return boolean
*/
function isIPv4($ip) {
return (preg_match('#^[0-9]{1,3}(\.[0-9]{1,3}){3}$#', $ip)) ? true : false;
}
/**
* Compares two ip by block compare.
* @param string $ip
* @param string $compareIP
* @param integer $blocks4 For IPv4
* @param integer $blocks6 For IPv6
* @return boolean
*/
function checkIP($ip, $compareIP, $blocks4 = 2, $blocks6 = 4) {
$return = true;
if (isIPv6($ip) && isIPv6($compareIP)) {
if ($blocks6 > 8) $blocks6 = 8;
$ip = fillZerosInIP($ip);
$compareIP = fillZerosInIP($compareIP);
$ipBlocks = explode(':', $ip);
$compareIPBlocks = explode(':', $compareIP);
for ($i = 0; $i < $blocks6; $i++) {
if ($ipBlocks[$i] != $compareIPBlocks[$i]) {
$return = false;
break;
}
}
}
elseif (isIPv4($ip) && isIPv4($compareIP)) {
if ($blocks4 > 4) $blocks4 = 4;
$ipBlocks = explode('.', $ip);
$compareIPBlocks = explode('.', $compareIP);
for ($i = 0; $i < $blocks4; $i++) {
if ($ipBlocks[$i] != $compareIPBlocks[$i]) {
$return = false;
break;
}
}
}
else {
$return = false;
}
return $return;
}
if (checkIP('::1:1', '::1:1') === false) echo 'falsche IP';
else echo 'Hey die IP stimmt';
?>
|
|
Lange ist es her, aber nun kommt auch mal wieder ein Code-Snippet von mir. Oben zu sehen der Quell-Text und dieser steht unter der GNU LGPLv3
Die Anwendung ist relativ einfach und eigentlich selbsterklärend. Die Funktionen isIPv4 und isIPv6 prüfen die IP auf das Internet Protokoll. Die Funktion fillZerosInIP füllt einen gekürzten Null-Block ( :: ) in einer IPv6-Adresse auf.
Mit der Funktion checkIP kann man zwei IPs vergleichen und so prüfen, ob eine IP übereinstimmt oder nicht. Dabei übergibt man mit den ersten beiden Parametern die IPs, die man prüfen möchte und mit dem dritten und vierten Parameter kann man die Genauigkeit einstellen.
Ich empfehle diese Einstellungen momentan noch auf 2 Blocks, bzw. 4 Blocks zu belassen, da es mit bestimmten ISP (u.a. AOL) dann unmöglich sein kann, die Seite zu besuchen, sollte der IP-Check für die Session genutzt werden.
|
Post wurde schon 3x editiert, das letzte mal am 01.03.2011 um 11:32 von Andavos
|
|