-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip.php
More file actions
73 lines (61 loc) · 1.48 KB
/
Copy pathip.php
File metadata and controls
73 lines (61 loc) · 1.48 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
date_default_timezone_set('UTC');
// parse_ipve($_SERVER['REMOTE_ADDR']);
function parse_ipv4($addr)
{
// http://stackoverflow.com/questions/12435582/php-serverremote-addr-shows-ipv6
// Known prefix
$v4mapped_prefix_hex = '00000000000000000000ffff';
$v4mapped_prefix_bin = pack("H*", $v4mapped_prefix_hex);
// Or more readable when using PHP >= 5.4
//$v4mapped_prefix_bin = hex2bin($v4mapped_prefix_hex);
// Parse
//$addr = $_SERVER['REMOTE_ADDR'];
$addr_bin = inet_pton($addr);
if( $addr_bin === FALSE ) {
// Unparsable? How did they connect?!?
die('Invalid IP address');
}
// Check prefix
if( substr($addr_bin, 0, strlen($v4mapped_prefix_bin)) == $v4mapped_prefix_bin) {
// Strip prefix
$addr_bin = substr($addr_bin, strlen($v4mapped_prefix_bin));
}
// Convert back to printable address in canonical form
$addr = inet_ntop($addr_bin);
return $addr;
}
//$_SERVER['REMOTE_ADDR']
//"::ffff:118.209.6.192"
$newip = parse_ipv4($_SERVER['REMOTE_ADDR']);
$newline = $newip . " - " . date("Ymd-H:i:s") . "\n";
$old = file_get_contents("ip.txt");
if ($old === FALSE){
echo 1;
exit;
}
if ($old == "")
{
if (file_put_contents("ip.txt", $newline) === FALSE) {
}
else {
echo 0;
exit;
}
}
$oldlines = explode("\n", $old);
$olddata = explode(" - ", $oldlines[0]);
$oldip = $olddata[0];
if ($oldip != $newip) {
$new = $newline . $oldlines[0];
}
else {
$new = $newline . $oldlines[1];
}
if (file_put_contents("ip.txt", $new) === FALSE)
{
echo 1;
}
else
echo 0;
?>