-3

I'm trying to get my users ip address when they open a certain page. I want this ip mailed to me or stored in the database.

At the moment I have this, but its not working.

<?php
$to = "flash1996mph@hotmail.com";
$subject = "test";
$ip = $_SERVER['REMOTE_ADDR'];
$txt = "Hello world!";

header ('mail($to,$subject,$txt,$ip);')
?>

I have no idea if this is right or wrong, I'm just a newby trying.. This was the first that came up in my mind.

All help is appreciated.

Thanks in advance,

-Kev

edit:

<?php
$to = "flash1996mph@hotmail.com";
$subject = "test";
$ip = $_SERVER['REMOTE_ADDR'];
$txt = "Hello world!";

 mail($to,$subject,$txt,$ip); 
?>

Got this now, and its still not working. Whenever i remove the "ip" variable it works. When i don't it doesn't send.

Kevin
  • 930
  • 3
  • 13
  • 34
  • 3
    ... why have you put `mail` inside of `header();`? Google "How to send emails with php" or something similar, and you'll find PLENTY of tutorials. – Epodax Jun 13 '16 at 11:48
  • 3
    @KevinAartsen I think you are misunderstanding the purpose of SO, we are not a free help desk and you are expected to put a certain amount of resources into this yourself, and if you don't know something then you better start learning because we aren't gonna teach you, further more, your current attitude is gonna leave very few willing to offer you any help. – Epodax Jun 13 '16 at 11:59
  • 5
    Possible duplicate of [How to send an email using PHP?](http://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php) – jmattheis Jun 13 '16 at 11:59
  • 2
    The reason for my initial question is because I've never seen it before and I was genuinely confused why you had done it like so, since I doubt that any tutorial you might have found would've instructed you to do it like that. Which led me to further believe that you had/have done very little research. – Epodax Jun 13 '16 at 12:03

2 Answers2

4

I think you have perfectly fetched the IP Adress of the user but the way you have called the mail function is completely wrong.

A better approach to do the task would be to create a function and pass the user's IP Address as and when required:

Create a file called functions.php and write the following code in it.

function fetch_user_IP($ip_address)
{

    $to = 'your-email@address.com';
    $subject = 'IP Address for the User';
    $message = 'Hi,<b/>Here is the IP Address: '.$ip_address.'<br/>Thanks';
    $headers = 'From: webmaster@example.com' . "\r\n" .
        'Reply-To: webmaster@example.com' . "\r\n" .


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

    //Insert the IP to Database
    //Write your DB query here
}

Now, from your PHP page call the function like

<?php fetch_user_IP($_SERVER['REMOTE_ADDR']);?>
Ashish Nayyar
  • 560
  • 3
  • 10
aniket ashtekar
  • 290
  • 1
  • 11
2

You should not put the mail sending inside header change to this, also the fourth parameter is for headers :

$txt = "Hello your ip is ".$ip;


 mail($to,$subject,$txt); 
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45