本文整理汇总了PHP中inet_pton函数的典型用法代码示例。如果您正苦于以下问题:PHP inet_pton函数的具体用法?PHP inet_pton怎么用?PHP inet_pton使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了inet_pton函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的PHP代码示例。
示例1: str_to_addr
private static function str_to_addr($ip)
{
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
throw new Exception("'{$ip}' is not a valid IPv6 address.");
}
return current(unpack("a16", inet_pton($ip)));
}
开发者ID:silvahq,项目名称:IPv6Calculator,代码行数:7,代码来源:ipv6calc.php
示例2: getLocation
public function getLocation($ip, $full)
{
//Check if the given IP Adress is valid
if (inet_pton($ip) !== false) {
$jsonLocation = file_get_contents('http://85.214.151.129:8081/json/' . (string) $ip);
//get the Contents of the server response (A JSON with all Location Data)
//if an error occurs with file_get_contents it returns false
//if there was no error with the request
if ($jsonLocation !== false) {
$arrLocation = json_decode($jsonLocation, true);
//Convert the JSON string into an array
//if the full location name was requested
if ($full === true) {
return $arrLocation['country_name'];
//return the full location name
} else {
return $arrLocation['country_code'];
//return the country code
}
}
} else {
//if the ip adress is not valid
return null;
//return null instead
}
}
开发者ID:eWert-Online,项目名称:ClickigniteSockets,代码行数:26,代码来源:miscFunctions.php
示例3: __construct
/**
* @param string ip
* @throws \Exception
*/
public function __construct($ip)
{
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
throw new \Exception("Invalid IP address format");
}
$this->in_addr = inet_pton($ip);
}
开发者ID:s1lentium,项目名称:iptools,代码行数:11,代码来源:IP.php
示例4: getClientIP
/**
* Returns the IP of the current client.
* The IP address is validated against the security validator. In case of an invalid IP, IP_INVALID is returned.
* @param bool Get the shorthand notation of an address.
* @return string The ip address of the client.
*/
public static final function getClientIP($shortHand = true)
{
//validate the handle to the server variable
$handle = self::getServerHandle();
$ip = self::IP_INVALID;
switch (true) {
case isset($handle['HTTP_CLIENT_IP']):
//if there is a HTTP_CLIENT_IP variable, we parse this and use the first value in it.
//this allows us to properly handle loadbalancers and cachers.
$ar = explode(',', $handle['HTTP_CLIENT_IP']);
$ip = trim(array_shift($ar));
break;
case isset($handle['HTTP_X_FORWARDED_FOR']):
//if there is a HTTP_X_FORWARDED_FOR variable, we parse this and use the first value in it.
//this allows us to properly handle loadbalancers and cachers.
$ar = explode(',', $handle['HTTP_X_FORWARDED_FOR']);
$ip = trim(array_shift($ar));
break;
case isset($handle['REMOTE_ADDR']):
//if there is a REMOTE_ADDR variable, we parse this and use the first value in it.
$ar = explode(',', $handle['REMOTE_ADDR']);
$ip = trim(array_shift($ar));
break;
}
$val = new \System\Security\Validate();
if ($val->isIPAddress($ip, 'ip', true, true, false, false, true) == \System\Security\ValidateResult::VALIDATE_OK) {
if ($shortHand) {
return inet_ntop(inet_pton($ip));
}
return $ip;
}
return self::IP_INVALID;
}
开发者ID:Superbeest,项目名称:Core,代码行数:39,代码来源:IP.class.php
示例5: doClean
/**
* @see sfValidatorBase
*/
protected function doClean($value)
{
if (@inet_pton($value)) {
return $value;
}
throw new sfValidatorError($this, 'invalid', array('value' => $value));
}
开发者ID:ketheriel,项目名称:ETVA,代码行数:10,代码来源:ValidatorIP.class.php
示例6: normalizeIp
/**
* Normalize an IP address or a beginning of it to an IPv6 address
*
* @param string $ip IP Address
* @param bool $end Whether to make a partial address an "end of range"
* address
*
* @return string|false Packed in_addr representation if successful, false
* for invalid IP address
*/
public function normalizeIp($ip, $end = false)
{
// The check for AF_INET6 allows fallback to IPv4 only if necessary.
// Hopefully that's not necessary.
if (strpos($ip, ':') === false || !defined('AF_INET6')) {
// IPv4 address
// Append parts until complete
$addr = explode('.', $ip);
for ($i = count($addr); $i < 4; $i++) {
$addr[] = $end ? 255 : 0;
}
// Get rid of leading zeros etc.
$ip = implode('.', array_map('intval', $addr));
if (!defined('AF_INET6')) {
return inet_pton($ip);
}
$ip = "::{$ip}";
} else {
// IPv6 address
// Expand :: with '0:' as many times as necessary for a complete address
$count = substr_count($ip, ':');
if ($count < 8) {
$ip = str_replace('::', ':' . str_repeat('0:', 8 - $count), $ip);
}
if ($ip[0] == ':') {
$ip = "0{$ip}";
}
// Append ':0' or ':ffff' to complete the address
$count = substr_count($ip, ':');
if ($count < 7) {
$ip .= str_repeat($end ? ':ffff' : ':0', 7 - $count);
}
}
return inet_pton($ip);
}
开发者ID:bbeckman,项目名称:NDL-VuFind2,代码行数:45,代码来源:IpAddressUtils.php
示例7: connect
public function connect($url, $cb)
{
$u = $this->parseUrl($url);
if (isset($u['user'])) {
$this->user = $u['user'];
}
$this->url = $url;
$this->scheme = $u['scheme'];
$this->host = $u['host'];
$this->port = $u['port'];
if (isset($u['pass'])) {
$this->password = $u['pass'];
}
if (isset($u['path'])) {
$this->path = ltrim($u['path'], '/');
}
if ($cb !== null) {
$this->onConnected($cb);
}
$conn = $this;
if ($this->port !== 0 && @inet_pton($this->host) === false) {
// dirty condition check
DNSClient::getInstance()->resolve($this->host, function ($real) use($conn) {
if ($real === false) {
Daemon::log(get_class($conn) . '->connectTo: enable to resolve hostname: ' . $conn->host);
return;
}
$conn->hostReal = $real;
$conn->connectTo($conn->hostReal, $conn->port);
});
} else {
$conn->hostReal = $conn->host;
$conn->connectTo($conn->hostReal, $conn->port);
}
}
开发者ID:ruslanchek,项目名称:phpdaemon,代码行数:35,代码来源:Connection.php
示例8: __construct
/**
* Socket constructor
*
* @param string $host Remote hostname
* @param int $port Remote port
* @param string $debugHandler Function to call for error logging
*/
public function __construct($host = 'localhost', $port = 9090, $debugHandler = null)
{
$this->host_ = $host;
$this->port_ = $port;
$this->ipV6_ = strlen(@inet_pton($host)) == 16;
$this->debugHandler_ = $debugHandler ?: 'error_log';
}
开发者ID:jamescarr,项目名称:fbthrift,代码行数:14,代码来源:TNonBlockingSocket.php
示例9: start
function start($user_id)
{
$data = array();
$data[":session_hash"] = sha1(rand());
// for now
if (strstr($_SERVER['REMOTE_ADDR'], ":")) {
// Remote address in in IPv6 notation
$data[":session_ip_start"] = inet_pton($_SERVER['REMOTE_ADDR']);
} else {
// Remote address in IPv4 notation
$data[":session_ip_start"] = inet_pton("::ffff:" . $_SERVER['REMOTE_ADDR']);
}
$data[":session_useragent"] = $_SERVER['HTTP_USER_AGENT'];
$sth = $this->stone->pdo->prepare("INSERT INTO session \n (session_hash,session_ip_start,session_useragent)\n values (:session_hash,:session_ip_start,:session_useragent)");
if (!$sth->execute($data)) {
//ERROR
}
// Set the cookie to the maximum 32 bit int value
// to prevent year 2038 problems.
setcookie("ItPhilManagerSession", $data[":session_hash"], 2147483647);
$data = array();
$data[':session_id'] = $this->stone->pdo->lastInsertId();
$data[':user_id'] = $user_id;
$sth = $this->stone->pdo->prepare("INSERT INTO link_session2user \n (session_id,user_id) \n VALUES (:session_id,:user_id)");
if (!$sth->execute($data)) {
//ERROR
}
}
开发者ID:The-IT-Philosopher,项目名称:manager,代码行数:28,代码来源:Session.class.php
示例10: is_ip
/**
* Check if string is a valid IP address
*
* @param $name string to check
* @return bool true if IP is valid
*/
function is_ip($name)
{
/*if(preg_match('/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/',$name))
return true;
return false;*/
return @inet_pton($name) !== false;
}
开发者ID:stweil,项目名称:OA-Statistik,代码行数:13,代码来源:oasparser.php
示例11: matches
/**
* check whether the expression matches an address
*
* @param AddressInterface $address
* @return boolean
*/
public function matches(AddressInterface $address)
{
$lower = $this->lower->getExpanded();
$addr = $address->getExpanded();
// http://stackoverflow.com/questions/594112/matching-an-ip-to-a-cidr-mask-in-php5
if ($address instanceof IPv4 && $this->lower instanceof IPv4) {
$addr = ip2long($addr);
$lower = ip2long($lower);
$netmask = -1 << 32 - $this->netmask & ip2long('255.255.255.255');
$lower &= $netmask;
return ($addr & $netmask) == $lower;
} elseif ($address instanceof IPv6 && $this->lower instanceof IPv6) {
$lower = unpack('n*', inet_pton($lower));
$addr = unpack('n*', inet_pton($addr));
for ($i = 1; $i <= ceil($this->netmask / 16); $i++) {
$left = $this->netmask - 16 * ($i - 1);
$left = $left <= 16 ? $left : 16;
$mask = ~(0xffff >> $left) & 0xffff;
if (($addr[$i] & $mask) != ($lower[$i] & $mask)) {
return false;
}
}
return true;
}
throw new \LogicException('Can only compare IPs of the same version.');
}
开发者ID:skosm,项目名称:LaraShop,代码行数:32,代码来源:Subnet.php
示例12: getBinary
public function getBinary()
{
if ($this->_binary === null) {
$this->_binary = inet_pton($this->_value);
}
return $this->_binary;
}
开发者ID:scriptbunny,项目名称:php-cassandra,代码行数:7,代码来源:Inet.php
示例13: reverseIPv6
static function reverseIPv6($ip)
{
$addr = inet_pton($ip);
$unpack = unpack('H*hex', $addr);
$hex = $unpack['hex'];
return implode('.', array_reverse(str_split($hex)));
}
开发者ID:W1zzardTPU,项目名称:XenForo_TPUDetectSpamReg,代码行数:7,代码来源:AS.php
示例14: get_cache
function get_cache($host, $value)
{
global $dev_cache;
if (!isset($dev_cache[$host][$value])) {
switch ($value) {
case 'device_id':
// Try by hostname
$ip = inet_pton($host);
if (inet_ntop($ip) === false) {
$dev_cache[$host]['device_id'] = dbFetchCell('SELECT `device_id` FROM devices WHERE `hostname` = ? OR `sysName` = ?', array($host, $host));
} else {
$dev_cache[$host]['device_id'] = dbFetchCell('SELECT `device_id` FROM devices WHERE `hostname` = ? OR `sysName` = ? OR `ip` = ?', array($host, $host, $ip));
}
// If failed, try by IP
if (!is_numeric($dev_cache[$host]['device_id'])) {
$dev_cache[$host]['device_id'] = dbFetchCell('SELECT `device_id` FROM `ipv4_addresses` AS A, `ports` AS I WHERE A.ipv4_address = ? AND I.port_id = A.port_id', array($host));
}
break;
case 'os':
$dev_cache[$host]['os'] = dbFetchCell('SELECT `os` FROM devices WHERE `device_id` = ?', array(get_cache($host, 'device_id')));
break;
case 'version':
$dev_cache[$host]['version'] = dbFetchCell('SELECT `version` FROM devices WHERE `device_id`= ?', array(get_cache($host, 'device_id')));
break;
default:
return null;
}
//end switch
}
//end if
return $dev_cache[$host][$value];
}
开发者ID:Tatermen,项目名称:librenms,代码行数:32,代码来源:syslog.php
示例15: log
/**
* Logs an action.
*
* @param string $action
* @param App\Board|String $board
* @param Array $data
* @return App\Log
*/
public function log($action, $board = null, $data = null)
{
$board_uri = null;
$action_details = null;
if ($board instanceof \App\Board) {
$board_uri = $board->board_uri;
$action_details = $data;
} else {
if ($board instanceof \App\Post) {
$board_uri = $board->board_uri;
$action_details = $data;
} else {
if (is_string($board)) {
$board_uri = $board;
$action_details = $data;
} else {
if (is_array($board) && is_null($data)) {
$board_uri = null;
$action_details = $board;
}
}
}
}
if (!is_null($action_details) && !is_array($action_details)) {
$action_details = [$action_details];
}
if (!is_null($action_details)) {
$action_details = json_encode($action_details);
}
$log = new Log(['action_name' => $action, 'action_details' => $action_details, 'user_id' => $this->user->isAnonymous() ? null : $this->user->user_id, 'user_ip' => inet_pton(Request::getClientIp()), 'board_uri' => $board_uri]);
return $log->save();
}
开发者ID:LulzNews,项目名称:infinity-next,代码行数:40,代码来源:Controller.php
示例16: get_ip
public static function get_ip()
{
static $ip;
if (!empty($ip)) {
return $ip;
}
$ip = !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : $_ENV['REMOTE_ADDR'];
$ips = array();
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != 'unknown') {
$ips = explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
}
if (!empty($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP'] != 'unknown') {
$ips[] = $_SERVER['HTTP_CLIENT_IP'];
}
for ($i = 0; $i < count($ips); $i++) {
if ($tmp = FILTER::ipv4($ips[$i])) {
$ip = $tmp;
break;
} else {
if ($tmp = FILTER::ipv6($ips[$i])) {
$ip = $tmp;
break;
}
}
}
$ip = inet_pton($ip);
return $ip;
}
开发者ID:cbsistem,项目名称:nexos,代码行数:28,代码来源:net.php
示例17: ip2dec
/**
* Convert an IP address from presentation to decimal(39,0) format suitable for storage in MySQL
*
* @param string $ip_address An IP address in IPv4, IPv6 or decimal notation
* @return string The IP address in decimal notation
*/
public function ip2dec($ip_address)
{
if (is_null($ip_address) === true) {
return null;
}
$ip_address = trim($ip_address);
// IPv4 address
if (strpos($ip_address, ':') === false && strpos($ip_address, '.') !== false) {
$ip_address = '::' . $ip_address;
}
// IPv6 address
if (strpos($ip_address, ':') !== false) {
$network = inet_pton($ip_address);
$parts = unpack('N*', $network);
foreach ($parts as &$part) {
if ($part < 0) {
$part = bcadd((string) $part, '4294967296');
}
if (!is_string($part)) {
$part = (string) $part;
}
}
$decimal = $parts[4];
$decimal = bcadd($decimal, bcmul($parts[3], '4294967296'));
$decimal = bcadd($decimal, bcmul($parts[2], '18446744073709551616'));
$decimal = bcadd($decimal, bcmul($parts[1], '79228162514264337593543950336'));
return $decimal;
}
// Decimal address
return $ip_address;
}
开发者ID:BGPView,项目名称:Backend-API,代码行数:37,代码来源:IpUtils.php
示例18: is_mptcp
function is_mptcp()
{
// Convert IP to packed in_addr form
$ip = inet_pton($_SERVER['REMOTE_ADDR']);
// Convert port to hex
$port_hex = strtoupper(dechex($_SERVER['REMOTE_PORT']));
// Split IP into array
$ip_hex = '';
$ip_array = str_split($ip);
// IPv4 conversion to hex
if (count($ip_array) === 4) {
// Iterate all characters, convert to hex
foreach ($ip_array as $c) {
$ip_hex = sprintf("%s%s", char2hex($c), $ip_hex);
}
} else {
// Iterate all characters, convert to hex
for ($i = 1; $i <= 4; $i++) {
for ($j = 4 * $i - 1; $j >= ($i - 1) * 4; $j--) {
$ip_hex = sprintf("%s%s", $ip_hex, char2hex($ip_array[$j]));
}
}
}
// Convert to uppercase
$ip_hex = strtoupper($ip_hex);
// Return if you're connected via MPTCP!
return strpos(shell_exec("cat /proc/net/mptcp"), $ip_hex . ':' . $port_hex) ? true : false;
}
开发者ID:rbauduin,项目名称:amiusingmptcp,代码行数:28,代码来源:mptcp.inc.php
示例19: __construct
public function __construct($uri)
{
$uri = (string) $uri;
if (!($parts = $this->parse($uri))) {
throw new \DomainException('Invalid URI specified at ' . get_class($this) . '::__construct Argument 1');
}
$this->uri = $uri;
foreach ($parts as $key => $value) {
$this->{$key} = $value;
}
// http://www.apps.ietf.org/rfc/rfc3986.html#sec-3.1
// "schemes are case-insensitive"
$this->scheme = strtolower($this->scheme);
// http://www.apps.ietf.org/rfc/rfc3986.html#sec-3.2.2
// "Although host is case-insensitive, producers and normalizers should use lowercase for
// registered names and hexadecimal addresses for the sake of uniformity"
$this->host = strtolower($this->host);
if ($this->port && $this->scheme) {
$this->normalizeDefaultPort();
}
$ip = @inet_pton($this->host);
if (isset($ip[5])) {
$this->isIpV6 = true;
} elseif ($ip) {
$this->isIpv4 = true;
}
$this->parseQueryParameters();
if ($this->fragment) {
$this->fragment = rawurldecode($this->fragment);
$this->fragment = rawurlencode($this->fragment);
}
}
开发者ID:amphp,项目名称:artax,代码行数:32,代码来源:Uri.php
示例20: cidrMatch
/**
* Matches a CIDR range pattern against an IP
*
* @param string $ip The IP to match
* @param string $range The CIDR range pattern to match against
* @return boolean TRUE if the pattern matched, FALSE otherwise
*/
protected function cidrMatch($ip, $range)
{
if (strpos($range, '/') === false) {
$bits = null;
$subnet = $range;
} else {
list($subnet, $bits) = explode('/', $range);
}
$ip = inet_pton($ip);
$subnet = inet_pton($subnet);
if ($ip === false || $subnet === false) {
return false;
}
if (strlen($ip) > strlen($subnet)) {
$subnet = str_pad($subnet, strlen($ip), chr(0), STR_PAD_LEFT);
} elseif (strlen($subnet) > strlen($ip)) {
$ip = str_pad($ip, strlen($subnet), chr(0), STR_PAD_LEFT);
}
if ($bits === null) {
return $ip === $subnet;
} else {
for ($i = 0; $i < strlen($ip); $i++) {
$mask = 0;
if ($bits > 0) {
$mask = $bits >= 8 ? 255 : 256 - (1 << 8 - $bits);
$bits -= 8;
}
if ((ord($ip[$i]) & $mask) !== (ord($subnet[$i]) & $mask)) {
return false;
}
}
}
return true;
}
开发者ID:patrickreck,项目名称:flow-development-collection,代码行数:41,代码来源:Ip.php
注:本文中的inet_pton函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论