我使用以下代码获取用户的IP并将其发送到我的电子邮件地址(我使用的是第三方电子邮件API):
<?php
$ip1 = $_SERVER['REMOTE_ADDR'];
$ip2 = $_SERVER['HTTP_X_FORWARDED_FOR']
$ip3 = $_SERVER['HTTP_FORWARDED'];
$ua = $_SERVER['HTTP_USER_AGENT'];
$to = 'abc@xyz.com';
$sub = 'test';
$msg = "$ip1, $ip2 and $ip3 on $ua \n ...other texts...";
$post = "key=blah&to=$to&sub=$sub&msg=$msg";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURL_POSTFIELDS, $post);
curl_setopt($ch, CURL_RETURNTRANSFER, 1);
$mailres = curl_exec ($ch);
?>
API只检索$ _POST数据并使用mail()发送电子邮件。
但是当我执行代码时,我得到的邮件的用户IP只存储在$ ip1中。例如,如果用户的IP是1.1.1.1,那么我只得到:
1.1.1.1,
没有发送用户代理和其他文本。可能是什么问题?
答案 0 :(得分:0)
首先尝试这种方式,只是为了确保你调试一些真实的东西:
$text = '';
if (isset($_SERVER['REMOTE_ADDR'])) {
$text .= $_SERVER['REMOTE_ADDR'].', ';
} else {
$text .= 'NO _SERVER[\'REMOTE_ADDR\'] HERE, ';
}
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$text .= $_SERVER['HTTP_X_FORWARDED_FOR'].', ';
} else {
$text .= 'NO _SERVER[\'HTTP_X_FORWARDED_FOR\'] HERE, ';
}
if (isset($_SERVER['HTTP_FORWARDED'])) {
$text .= $_SERVER['HTTP_FORWARDED'].', ';
} else {
$text .= 'NO _SERVER[\'HTTP_FORWARDED\'] HERE, ';
}
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$text .= ' on '.$_SERVER['HTTP_USER_AGENT'].', ';
} else {
$text .= 'NO _SERVER[\'HTTP_USER_AGENT\'] HERE, ';
}
$text .= "\n ...other texts...";
mail ($to, $sub, $text );
但是卷曲应该是:
$post = array("key"=>'blah',
'to'=>$to,
'sub'=>$sub,
'msg'=>$msg);
和
curl_setopt($ch, CURL_POSTFIELDS, json_encode($post));