Làm cách nào để xác định địa chỉ IP của máy chủ trong PHP?

The problem of broken DNS servers was causing me a problem because i had a page for user statistics that required around 20 reverse dns lookups to be done, and even as many as 5/6 of them being broken was causing a huge delay in loading the page. so i wrote a function that uses a UDP socket to talk directly to the DNS server [instead of going via the normal gethostbyaddr function] this let me set a timeout.

The only requirement is that your DNS server must be able to do recursive lookups, it wont go to other DNS servers if its told to.. and of course you need to know your DNS servers IP address :-]

function gethostbyaddr_timeout[$ip, $dns, $timeout=1000]
{
    // random transaction number [for routers etc to get the reply back]
    $data = rand[0, 99];
    // trim it to 2 bytes
    $data = substr[$data, 0, 2];
    // request header
    $data .= "\1\0\0\1\0\0\0\0\0\0";
    // split IP up
    $bits = explode[".", $ip];
    // error checking
    if [count[$bits] != 4] return "ERROR";
    // there is probably a better way to do this bit...
    // loop through each segment
    for [$x=3; $x>=0; $x--]
    {
        // needs a byte to indicate the length of each segment of the request
        switch [strlen[$bits[$x]]]
        {
            case 1: // 1 byte long segment
                $data .= "\1"; break;
            case 2: // 2 byte long segment
                $data .= "\2"; break;
            case 3: // 3 byte long segment
                $data .= "\3"; break;
            default: // segment is too big, invalid IP
                return "INVALID";
        }
        // and the segment itself
        $data .= $bits[$x];
    }
    // and the final bit of the request
    $data .= "\7in-addr\4arpa\0\0\x0C\0\1";
    // create UDP socket
    $handle = @fsockopen["udp://$dns", 53];
    // send our request [and store request size so we can cheat later]
    $requestsize=@fwrite[$handle, $data];

    @socket_set_timeout[$handle, $timeout - $timeout%1000, $timeout%1000];
    // hope we get a reply
    $response = @fread[$handle, 1000];
    @fclose[$handle];
    if [$response == ""]
        return $ip;
    // find the response type
    $type = @unpack["s", substr[$response, $requestsize+2]];
    if [$type[1] == 0x0C00]  // answer
    {
        // set up our variables
        $host="";
        $len = 0;
        // set our pointer at the beginning of the hostname
        // uses the request size from earlier rather than work it out
        $position=$requestsize+12;
        // reconstruct hostname
        do
        {
            // get segment size
            $len = unpack["c", substr[$response, $position]];
            // null terminated string, so length 0 = finished
            if [$len[1] == 0]
                // return the hostname, without the trailing .
                return substr[$host, 0, strlen[$host] -1];
            // add segment to our host
            $host .= substr[$response, $position+1, $len[1]] . ".";
            // move pointer on to the next segment
            $position += $len[1] + 1;
        }
        while [$len != 0];
        // error - return the hostname we constructed [without the . on the end]
        return $ip;
    }
    return $ip;
}
?>

This could be expanded quite a bit and improved but it works and i've seen quite a few people trying various methods to achieve something like this so i decided to post it here. on most servers it should also be more efficient than other methods such as calling nslookup because it doesn't need to run external programs

Note: I'm more a C person than a PHP person, so just ignore it if anything hasn't been done the *recomended* way :-]

PHP có thể được sử dụng để lấy Địa chỉ IP của khách truy cập trang web, địa chỉ IP của máy chủ lưu trữ trang web và phân giải địa chỉ IP để lấy tên máy chủ. k. một bản ghi DNS đảo ngược. PHP đã xác định trước biến mảng $_SERVER bằng cách sử dụng địa chỉ IP của cả khách truy cập và máy chủ có thể được truy xuất. Hàm gethostbyaddr[] được sử dụng để tìm ra tên máy chủ. Các chỉ số REMOTE_ADDR và ​​HTTP_HOST chứa địa chỉ IP của khách truy cập và địa chỉ IP của máy chủ Để tìm địa chỉ IP của khách truy cập bằng PHP, hãy sử dụng biến $_SERVER[‘REMOTE_ADDR’]. Ví dụ, đoạn mã sau

sẽ hiển thị địa chỉ IP của khách truy cập cho chính khách truy cập. Nếu bạn muốn ghi lại địa chỉ IP này, hãy chèn giá trị vào cơ sở dữ liệu

Để biết địa chỉ IP của máy chủ web của bạn bằng PHP, hãy sử dụng biến $_SERVER[‘HTTP_HOST’]

Để lấy tên máy chủ của biến, hãy sử dụng hàm gethostbyaddr[] với địa chỉ IP làm tham số. Ví dụ

Chủ Đề