• Czas czytania ~1 min
  • 24.08.2022

Pakiet darsyn/ip autorstwa Zana Baldwina jest obiektem wartości niezmiennej dla adresów IPv4 i IPv6. Biblioteka zawiera kilka metod pomocniczych dla zakresów adresów IP, adresów emisji i sieci, masek podsieci oraz tego, czy adres IP jest określonego typu.

Oto krótki przykład , który pokazuje niektóre funkcje tego pakietu:

use Darsyn\IP\Exception;
use Darsyn\IP\Version\IPv4;

try {
    $ip = IPv4::factory('192.168.0.1');
} catch (Exception\InvalidIpAddressException $e) {
    exit('The IP address supplied is invalid!');
}
$companyNetwork = IPv4::factory('216.58.198.174');
if (!$ip->inRange($companyNetwork, 25)) {
    throw new \Exception('Request not from a known company IP address.');
}
// Is it coming from the local network?
if (!$ip->isPrivateUse()) {
    record_visit($ip->getBinary(), $_SERVER['HTTP_USER_AGENT']);
}

Here are some more examples of what you can do:

use Darsyn\IP\Version\Multi as IP;

$ip = IP::factory('127.0.0.1');

// string("::ffff:7f00:1")
echo $ip->getCompactedAddress();

// string("0000:0000:0000:0000:0000:ffff:7f00:0001")
$ip->getExpandedAddress();

$ip = IP::factory('::ffff:7f00:1');
// string("127.0.0.1")
$ip->getProtocolAppropriateAddress();

$ip = IP::factory('127.0.0.1');
// Returns the 16 byte (4 bytes for IPv4) binary string
$binary = $ip->getBinary();

I'd recommend reading the documenation to get familiar with this package and its features. You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

O

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

O autorze CrazyBoy49z
WORK EXPERIENCE
Kontakt
Ukraine, Lutsk
+380979856297