mirror of
https://github.com/YGGverse/net-php.git
synced 2026-03-31 17:15:35 +00:00
implement port open check
This commit is contained in:
parent
8eba81ae5f
commit
673aacdb31
1 changed files with 43 additions and 0 deletions
43
src/Port.php
Normal file
43
src/Port.php
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Yggverse\Net;
|
||||||
|
|
||||||
|
class Port
|
||||||
|
{
|
||||||
|
public static function isHost(mixed $value): bool
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(
|
||||||
|
is_string($value) &&
|
||||||
|
(
|
||||||
|
false !== filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ||
|
||||||
|
false !== filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ||
|
||||||
|
false !== filter_var($value, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isPort(mixed $value): bool
|
||||||
|
{
|
||||||
|
$length = strlen(
|
||||||
|
$value
|
||||||
|
);
|
||||||
|
|
||||||
|
return false !== filter_var($value, FILTER_VALIDATE_INT) && 0 < $length && 65536 > $length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isOpen(string $host, int $port = -1, ?float $timeout = null, int &$error_code = null, string &$error_message = null): bool
|
||||||
|
{
|
||||||
|
return is_resource(
|
||||||
|
@fsockopen(
|
||||||
|
$host,
|
||||||
|
$port,
|
||||||
|
$error_code,
|
||||||
|
$error_message,
|
||||||
|
$timeout
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue