获取IP并通过电子邮件发送

时间:2014-02-21 08:21:42

标签: php button ip submit

我有这个简单的代码:

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

我希望在字段中显示一个IP号码,以便直接将其发送到电子邮件中。输入 file.php 的人在现场看到他们的IP,并且能够按下“发送”按钮以便将其发送给我。 感谢。

2 个答案:

答案 0 :(得分:1)

<?php
$ip=$_SERVER['REMOTE_ADDR'];
mail ('me@gmail.com', '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


?>

答案 1 :(得分:1)

以下代码已完成且有效,只需替换示例电子邮件用户名和密码以及smtp服务器,并将以下所有代码保存在.php文件中。

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

$host = "smtp.EXAMPLE.com";
$port = "25";
$username = "EXAMPLE@EXAMPLE.EXAMPLE";
$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");

?>