2013-07-09 215 views
1

我有一臺服務器(http://www.xxxx.com/php/)的同一目錄中的兩個PHP文件,PHP,獲得客戶端的IP地址

1)write_json.php

 


    $address['http_client'] = $_SERVER['HTTP_CLIENT_IP']; 
    $address['http_x_forwarded_for'] = $_SERVER['HTTP_X_FORWARDED_FOR']; 
    $address['http_x_forwarded'] = $_SERVER['HTTP_X_FORWARDED']; 
    $address['http_forwarded_for'] = $_SERVER['HTTP_FORWARDED_FOR']; 
    $address['http_forwarded'] = $_SERVER['HTTP_FORWARDED']; 
    $address['remote_addr'] = $_SERVER['REMOTE_ADDR']; 

    header('Content-Type: application/json'); 
    echo json_encode($address); 

2)get_ip.php

 
    $json_url = $_SERVER['SERVER_NAME'] . '/php/write_json.php'; 

    $json_string = ''; 
    $ch = curl_init($json_url); 


    $options = array(CURLOPT_RETURNTRANSFER => true, 
         CURLOPT_HTTPHEADER => array('Content-type: application/json'), 
         CURLOPT_POSTFIELDS => $json_string); 

    curl_setopt_array($ch, $options); 

    $json_string = curl_exec($ch); 
    echo $json_string; 

get_ip.php    個電話    write_json.php

如果我們假定客戶端IP爲:76.2.35.46和服務器之一是:80.59.3.23
當我打電話http://www.xxxx.com/php/get_ip.php在客戶端瀏覽器

這表明我不是服務器IP客戶端IP是這樣的:

 
{ 
    http_client: null, 
    http_x_forwarded_for: null, 
    http_x_forwarded: null, 
    http_forwarded_for: null, 
    http_forwarded: null, 
    remote_addr: "80.59.3.23" 
} 

我怎樣才能獲取客戶端IP而不是服務器?

回答

2

您正在通過curl從服務器調用write_json ...也就是說,服務器實際上是在請求write_json文件,所以write_json看到請求來自服務器。爲什麼不使用包含而不是捲曲調用呢?

0

不可能這樣。您需要先獲取客戶端IP,然後通過curl將其作爲發佈數據發送到服務器。