Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

PHP Contact Form - Obtain IP Address

I have a PHP contact form on my site which does work. I want a way to get a users IP address discretly sent to my email when the click SUBMIT on my form. I have tried various lines of code to do this to no avail. I am a novice so can someone please send me some step-by-step instructions on how to do this? That would be greatly appreciated.

Thanks,
aaronmagic
0
aaronmagic
Asked:
aaronmagic
  • 4
  • 2
1 Solution
 
ncooCommented:
<?php
$ip = $_SERVER['REMOTE_ADDR'];// returns IP.
$message = "your message\nIP is ".$ip;
$email = "from@email.com";

 mail( "yourname@example.com", "Subject",
    $message, "From: $email" );

?>
0
 
ncooCommented:
Full code would be:

email.htm
=======
<form method="post" action="sendmail.php">
  Email: <input name="email" type="text" /><br />
  Message:<br />
  <textarea name="message" rows="15" cols="40">
  </textarea><br />
  <input type="submit" />
</form>

sendmail.php
==========
<?
  $email = $_REQUEST['email'] ;
  $message = $_REQUEST['message'] ;
  $ip = $_SERVER['REMOTE_ADDR'];// returns IP.
  $message .= "\nIP is ".$ip;

  mail( "yourname@example.com", "Web Mail",
    $message, "From: $email" );
  header( "Location: http://www.example.com/thankyou.html" );
?>
0
 
aaronmagicAuthor Commented:
I must be doing something wrong. I added the code you said but when I click submit I still get the form but no IP address. Here is what my code looks like. Can you please alter it to fit?

Thanks,
aaronmagic

<?php

$sendTo = "admin@thisisatest.com";
$subject = "My Flash site reply";


 while ($request = current($_REQUEST)) {
       if (key($_REQUEST)!='recipient') {
            $pre_array=split ("&777&",  $request);
            $post_vars[key($_REQUEST)][0]=$pre_array[0];
            $post_vars[key($_REQUEST)][1]=$pre_array[1];
      }
      next($_REQUEST);
}

reset($post_vars);
$subject="From ".$post_vars['your_name'][0] ;
$headers= "From: ".$post_vars['your_email'][0] ."\n";
 $headers.='Content-type: text/html; charset=iso-8859-1';
 $message='';
  while ($mess = current($post_vars)) {
        if ((key($post_vars)!="i") && (key($post_vars)!="your_email") && (key($post_vars)!="your_name")) {

             $message.="<strong>".$mess[1]."</strong>&nbsp;&nbsp;&nbsp;".$mess[0]."<br>";
      }
      next($post_vars);
 }


mail($sendTo, $subject, $message, $headers);
?>
"Your message has been sent!"
<script>
      resizeTo(300, 300);
</script>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ncooCommented:
//add the following just before mail()
$ip = $_SERVER['REMOTE_ADDR'];// returns IP.
$message .= "\nIP is ".$ip;

mail($sendTo, $subject, $message, $headers);
0
 
ncooCommented:
Just read the question again, this will display it in the browser the previous method emailed it to you.

"Your message has been sent from IP <?php echp $_SERVER['REMOTE_ADDR']; ?>!"
<script>
      resizeTo(300, 300);
</script>
0
 
aaronmagicAuthor Commented:
Beautiful!!!! Adding

$ip = $_SERVER['REMOTE_ADDR'];// returns IP.
$message .= "\nIP is ".$ip;

Did the trick. TY!!! Much appreciated!!!

- aaronmagic
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now