简体   繁体   中英

how to track ip address of email sender

I am using mail function in php, how to track or find the IP address of email sender. here is my code.

mail($to,$subject,$message);

all parameter came from view page using $_POST .

Check this answer https://stackoverflow.com/a/3358212/829533

Make a function in php

function getUserIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) //if from shared
    {
        return $_SERVER['HTTP_CLIENT_IP'];
    }
    else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //if from a proxy
    {
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        return $_SERVER['REMOTE_ADDR'];
    }
}

and add ip address to the email

$message = "IP Address: " . getUserIpAddr();
mail($to,$subject,$message);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM