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
aaronmagicAsked:
Who is Participating?
 
ncooConnect With a Mentor Commented:
//add the following just before mail()
$ip = $_SERVER['REMOTE_ADDR'];// returns IP.
$message .= "\nIP is ".$ip;

mail($sendTo, $subject, $message, $headers);
0
 
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.