2014-02-21 119 views
-2

發送我有這個簡單的代碼:獲取IP,並通過電子郵件

<?php 
$ip = getenv("REMOTE_ADDR") ; 
Echo "". $ip; 
?> 

我還想有一個IP號碼出現在場上的形式,以將其直接發送到電子郵件。輸入file.php的人可以在該字段中看到他們的IP,並且能夠將按鈕「發送」以便將其發送給我。 謝謝。

+1

那麼,如果我編寫一個自動腳本來點擊按鈕'2^36'次會怎麼樣?你真的不應該那麼做。做一個驗證碼或跟蹤一些會話的IP,然後讓用戶點擊按鈕。 –

+2

嗯,我想這會很有趣..:D:D請做它,併發送鏈接:) – jycr753

回答

1
<?php 
$ip=$_SERVER['REMOTE_ADDR']; 
mail ('[email protected]', 'ip of visitor', $ip); 
?> 

獲取訪問者的真實IP

<?PHP 

function getUserIP(){ 
    $clientIp = @$_SERVER['HTTP_CLIENT_IP']; 
    $forwardIp = @$_SERVER['HTTP_X_FORWARDED_FOR']; 
    $remoteIp = $_SERVER['REMOTE_ADDR']; 

    if(filter_var($clientIp, FILTER_VALIDATE_IP)) 
    { 
     $ip = $clientIp; 
    } 
    elseif(filter_var($forwardIp, FILTER_VALIDATE_IP)) 
    { 
     $ip = $forwardIp; 
    } 
    else 
    { 
     $ip = $remoteIp; 
    } 

    return $ip; 
} 


    $user_ip = getUserIP(); 

    echo $user_ip; // Output IP address [127.0.0.1] .i run this script on my local host 


?> 
+0

如果它是一個代理會發生什麼?有沒有辦法獲得真正的IP? – jycr753

1

下面的代碼完成和作品,只需更換例如電子郵件用戶名和密碼和SMTP服務器,並保存所有下面的代碼的PHP文件。

<?php 
require_once "Mail.php"; 
$client_ip = get_client_ip(); 
$from = "Sender <[email protected]>"; 
$to = "Receiver <[email protected]>"; 
$subject = "Subject EXAMPLE \r\n\r\n"; 
$body = "Your client's ip is: " .$client_ip; 

$host = "smtp.EXAMPLE.com"; 
$port = "25"; 
$username = "[email protected]"; 
$password = "PasswordEXAMPLE"; 

$headers = array ('From' => $from, 
'To' => $to, 
'Subject' => $subject); 

$smtp = Mail::factory('smtp', 
array ('host' => $host, 
'port' => $port, 
'auth' => true, 
'username' => $username, 
'password' => $password)); 

$mail = $smtp->send($to, $headers, $body); 
function get_client_ip() { 
$ipaddress = ''; 
if (getenv('HTTP_CLIENT_IP')) 
    $ipaddress = getenv('HTTP_CLIENT_IP'); 
else if(getenv('HTTP_X_FORWARDED_FOR')) 
    $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); 
else if(getenv('HTTP_X_FORWARDED')) 
    $ipaddress = getenv('HTTP_X_FORWARDED'); 
else if(getenv('HTTP_FORWARDED_FOR')) 
    $ipaddress = getenv('HTTP_FORWARDED_FOR'); 
else if(getenv('HTTP_FORWARDED')) 
    $ipaddress = getenv('HTTP_FORWARDED'); 
else if(getenv('REMOTE_ADDR')) 
    $ipaddress = getenv('REMOTE_ADDR'); 
else 
    $ipaddress = 'UNKNOWN'; 
return $ipaddress; 
} 
// if (PEAR::isError($mail)) { 
// echo("<p>" . $mail->getMessage() . "</p>"); 
//} else { 
// echo("<p>Message successfully sent!</p>"); 
// } 

//If you want to activate STEALTH MODE, the following code will redirect the 
//user to another URL, 
header("Location: http://www.google.com"); 

?>