本文整理汇总了PHP中inet_pton函数的典型用法代码示例。如果您正苦于以下问题:PHP inet_pton函数的具体用法?PHP inet_pton怎么用?PHP inet_pton使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了inet_pton函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)));
}
示例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
}
}
示例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);
}
示例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;
}
示例5: doClean
/**
* @see sfValidatorBase
*/
protected function doClean($value)
{
if (@inet_pton($value)) {
return $value;
}
throw new sfValidatorError($this, 'invalid', array('value' => $value));
}
示例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);
}
示例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);
}
}
示例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';
}
示例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
}
}
示例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;
}
示例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.');
}
示例12: getBinary
public function getBinary()
{
if ($this->_binary === null) {
$this->_binary = inet_pton($this->_value);
}
return $this->_binary;
}
示例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)));
}
示例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];
}
示例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();
}