Link to home
Start Free TrialLog in
Avatar of aaronmagic
aaronmagic

asked on

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
Avatar of ncoo
ncoo

<?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" );

?>
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" );
?>
Avatar of aaronmagic

ASKER

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>
ASKER CERTIFIED SOLUTION
Avatar of ncoo
ncoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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>
Beautiful!!!! Adding

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

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

- aaronmagic