Ophalen van IP en alle informatie daarrond

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

G P

G P

18/09/2014 14:53:31
Quote Anchor link
Hallo,

ik heb een class geschreven om het IP op te halen en alle informatie daarrond (dns, host, whois, enz...)
Toch zou ik jullie reacties willen horen hoe het beter kan of wat er fout is.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php

/**************************************************\
    Get info from IP
\**************************************************/

class ip
{
    // Public
    // Private
    // Protected

    // Construct

    function __construct($ip = NULL){
        // Put all headers in an array
        $fHeaders = array(
            'HTTP_PRAGMA',
            'HTTP_XONNECTION',
            'HTTP_CACHE_INFO',
            'HTTP_XPROXY',
            'HTTP_PROXY',
            'HTTP_PROXY_CONNECTION',
            'HTTP_CLIENT_IP',
            'HTTP_VIA',
            'HTTP_X_COMING_FROM',
            'HTTP_X_FORWARDED_FOR',
            'HTTP_X_FORWARDED',
            'HTTP_COMING_FROM',
            'HTTP_FORWARDED_FOR',
            'HTTP_FORWARDED',
            'REMOTE_ADDR',
            'SERVER_ADDR',
            'ZHTTP_CACHE_CONTROL',
        );

        // Loop trough all the headers
        foreach ($fHeaders as $fClient){
            $_SERVER[$fClient] = isset($_SERVER[$fClient]) ? $_SERVER[$fClient] : NULL;
            $ip = @getenv($fClient) ? trim(@getenv($fClient)) : trim($_SERVER[$fClient]);
            // Check IP is set
            if (!empty($ip)){
                // Get host
                if ($ip == '::1'){
                    $url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
                    $ip = '127.0.0.1';
                }
else {
                    $url = gethostbyaddr($ip);
                }

                $this->info[$fClient]['host'] = isset($this->info[$fClient]['host']) ? $this->info[$fClient]['host'] : NULL;
                $this->info[$fClient]['path'] = isset($this->info[$fClient]['path']) ? $this->info[$fClient]['path'] : NULL;
                while(empty($this->info[$fClient]['host']) && empty($this->info[$fClient]['path'])){
                    $this->info[$fClient] = parse_url($url);
                    if (empty($this->info[$fClient]['host']) && empty($this->info[$fClient]['path'])){ $url = '/'.$url; }
                }

                $this->info[$fClient]['ip'] = isset($this->info[$fClient]['ip']) ? $this->info[$fClient]['ip'] : $ip;
                $this->info[$fClient]['path'] = isset($this->info[$fClient]['path']) ? ltrim($this->info[$fClient]['path'], '/') : NULL;
                $this->info[$fClient]['host'] = isset($this->info[$fClient]['host']) ? $this->info[$fClient]['host'] : $url;
                // Get File
                $explode = explode('/', $this->info[$fClient]['path']);
                $file = end($explode);
                if (count(explode('.', $file)) < 2){
                    unset($file);
                }
else {
                    $this->info[$fClient]['path'] = rtrim(str_replace($file, '', $this->info[$fClient]['path']), '/');
                    $explode = explode('.', $file);
                    $this->info[$fClient]['file']['extention'] = end($explode);
                    $this->info[$fClient]['file']['filename'] = str_replace('.'.$this->info[$fClient]['file']['extention'], '', $file);
                }

                // Get Keys in Query
                if (isset($this->info[$fClient]['query'])){
                    $keys = explode('&', $this->info[$fClient]['query']);
                    foreach($keys as $key => $value){
                        $exp = explode('=', $value);
                        $this->info[$fClient]['key'][$exp[0]] = $exp[1];
                    }
                }

                $this->info[$fClient]['user'] = isset($this->info[$fClient]['user']) ? $this->info[$fClient]['user'] : NULL;
                $this->info[$fClient]['pass'] = isset($this->info[$fClient]['pass']) ? $this->info[$fClient]['pass'] : NULL;
                // Get Domainname
                $this->info[$fClient]['domain'] = !empty($this->info[$fClient]['domain']) ? $this->info[$fClient]['domain'] : trim($this->info[$fClient]['host']);
                $this->info[$fClient]['domain'] = preg_replace('/^(http:\/\/)*(www.)*/is', '', $this->info[$fClient]['domain']);
                $this->info[$fClient]['domain'] = preg_replace('/\/.*$/is', '', $this->info[$fClient]['domain']);
                $parts = explode('.', $this->info[$fClient]['domain']);
                while (array_shift($parts) !== NULL){
                    $hostname = trim(implode(".", $parts));
                    if (!empty($hostname) && checkdnsrr($hostname, "A")) { $this->info[$fClient]['domain'] = $hostname; }
                }

                // Get WhoIs
                if ($ip != '127.0.0.1') { exec("whois $ip", $this->info[$fClient]['whois']); }
                if (!empty($this->info[$fClient]['whois'])){
                    $cleanWhois = $this->info[$fClient]['whois'];
                    foreach ($cleanWhois as $key => $val){
                        if (empty($val)){
                            unset($cleanWhois[$key]);
                        }
else {
                            $exp = explode(':', $val);
                            if (count($exp) == 2){
                                unset($cleanWhois[$key]);
                                $newKey = trim($exp[0]);
                                $newVal = trim($exp[1]);
                                if (is_numeric($newKey)){
                                    $cleanWhois[] = $newVal;
                                }
else {
                                    $cleanWhois[$newKey] = $newVal;
                                }
                            }
else {
                                $cleanWhois[$key] = trim($val);
                            }
                        }
                    }

                    ksort($cleanWhois);
                    $this->info[$fClient]['whois'] = $cleanWhois;
                }

                // Get DNS
                $this->info[$fClient]['dns'] = (!empty($this->info[$fClient]['domain']) && $ip!='127.0.0.1') ? dns_get_record($this->info[$fClient]['domain'], DNS_ALL) : NULL;
                // Clean Empty Values
                $this->info[$fClient] = $this->clean($this->info[$fClient]);
                // Sort Array on Key
                ksort($this->info[$fClient]);
            }
else {
                // Useless
                unset($this->info[$fClient]);
            }
        }
    }


    // Function to Clean Empty Values
    private function clean($array){
        foreach ($array as $key => $value){
            if (!is_array($value)){
                $value = trim($value);
                if (empty($value)){ unset($array[$key]); }
            }
else {
                $value = $this->clean($value);
            }
        }

        return $array;
    }

}


$info = new ip;
print '<pre>';
print_r($info);
print '</pre>';

?>
 
Er zijn nog geen reacties op dit bericht.



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.