-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathfunction.php
More file actions
45 lines (35 loc) · 994 Bytes
/
function.php
File metadata and controls
45 lines (35 loc) · 994 Bytes
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
<?php
if (!function_exists('filterInputPostGet')) {
function filterInputPostGet(string $name, ?string $default = null): ?string
{
$value = $_POST[$name] ?? $_GET[$name] ?? null;
if ($value === null) {
return $default;
}
return trim($value);
}
}
function getIP(): string
{
$manualIP = filterInputPostGet('ip');
if (!empty($manualIP)) {
$ip = $manualIP;
} else {
$ip = $_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '';
$ip = trim(explode(',', $ip)[0]);
}
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
throw new Exception('No valid IP');
}
return $ip;
}
function getIPType(string $ip): string
{
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return 'A';
}
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return 'AAAA';
}
throw new Exception('Unknown IP type');
}