Code or Script to Email IP Address

Apple / G5
January 12, 2009 at 13:11:38
Specs: OS X 10.4.11, Dual 2.5 GHz
Hi, I'm somewhat new to the whole web design thing and was wondering if there was any way to put some code or a script into a web page that, when someone with the IP Address xx.xx.xx.xx hits that particular page, the code or script would automatically send an email at the time of the hit?

Is this possible? Can anyone post the code?

Thanks in advance.

See More: Code or Script to Email IP Address

Report •

January 14, 2009 at 11:24:39
With php, you could possibly try the mail function

   if(strcmp($_SERVER["REMOTE_ADDR"], "") == 0) {
      $name = "sender's name";
      $from = "from@host";
      $sendto = "to@host";
      $body = "the page got a hit from:\r\n" . 
              $_SERVER["REMOTE_ADDR"] . "\r\n" .
              gethostbyaddr($_SERVER["REMOTE_ADDR"]) . "\r\n" .
              $_SERVER["HTTP_USER_AGENT"] . "\r\n" .
              date('M d, Y h:i a', time()) . "\r\n";
      $subject = "hit from target site";
      $header = "From: ". $name . " <" . $from . ">\r\n";
      mail($sendto, $subject, $body, $header);
rest of script

The second parameter in strcmp should be that of the ip you're looking for. I have no ideas on other scripting languages.


Report •

January 19, 2009 at 12:06:07
It took me a while to figure out how to use php. I'm still quite foggy on it all, BUT the important thing is, after much trial and error trying to figure out how to apply your script, I did it! Thank you SOOOO much for your help in this. It was driving me crazy, and I finally figured out where to put what. Again, thank you for your help.

Report •

January 19, 2009 at 13:22:17
I hate to ask for more, but I'm really excited that it's working. How would I input variables for more than one IP Address? For instance, I have it set to alert me when a specific IP address hits my page, but I'd also like to find out when an IP Address that starts out as (for instance) "32.143.xx.xx" hits my page. What are the variables or wildcards for the other digits in the IP Address that would allow me to do that?

Again, thank you so much for your help. I'm still learning (teaching myself through googling and trial and error) so please for give my thickness to what may be a very simple answer.

Report •

Related Solutions

January 22, 2009 at 14:04:52

   define("ADD1",  32);
   define("ADD2", 143);

   $ip = array("", "", "", "",
               "", "", "");

   foreach($ip as $val) {   
      $pat = '/(' . ADD1 . '\.' . ADD2 . ')\.([[:alnum:]]+)\.([[:alnum:]]+)/';
      echo $val . (preg_match($pat, $val) > 0 ? " <strong>true</strong>" : " false") . "


I *think* something like above will work; I made an array to test ips, but you wouldn't need the foreach loop or the array.


 if(strcmp($_SERVER["REMOTE_ADDR"], "") == 0) {

 if(preg_match($pat, $_SERVER["REMOTE_ADDR"]) > 0) {
   // add mail routine here

I used define to "fix" part of the ip, but you can use variables too.


Report •

Ask Question