【问题标题】:get ip and send it by email获取 ip 并通过电子邮件发送
【发布时间】:2014-03-22 15:29:51
【问题描述】:

我有这个简单的代码:

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

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

【问题讨论】:

  • 那么如果我编写一个自动化脚本来点击按钮2^36 次会发生什么?您实际上不应该这样做.. 使用一些会话制作验证码或跟踪 IP,然后让用户单击按钮。
  • 嗯,我想这会很有趣.. :D :D 请这样做并发送链接:)

标签: php button ip submit


【解决方案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


?>

【讨论】:

  • 如果它是代理会发生什么?有没有办法获得真实的ip?
【解决方案2】:

下面的代码是完整的并且可以运行,只需替换示例电子邮件用户名和密码以及 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");

?>

【讨论】:

    猜你喜欢
    • 2014-04-11
    • 2015-12-07
    • 2012-10-25
    • 2014-10-29
    • 2018-05-21
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    相关资源
    最近更新 更多