How to track the IP Address of the clients who post the feedback?

Discussion in 'PHP' started by Ngajel, Jul 29, 2010.

  1. #1
    I need guidelines/code of tracing the ip address of the clients who post the feedback.

    Please help me.

    Thanks
    Wangdi
     
    Ngajel, Jul 29, 2010 IP
  2. Johnta

    Johnta Peon

    Messages:
    101
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    getenv(REMOTE_ADDR); for the IP Address of the visitor
     
    Johnta, Jul 29, 2010 IP
  3. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?php
        $client_ip = $_SERVER['REMOTE_ADDR'];
    ?>
    
    PHP:
    There are some nice web based API's (to save you having the database) that will also return the users location based on the ip address. :)
     
    Deacalion, Jul 29, 2010 IP
  4. Ngajel

    Ngajel Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Dear Johnta,
    Thanks a lot for your response, i have done exactly with your code, but in my database table i am getting ip address as 127.0.0.1, but when i check to my computer i couldn't find the above ip address. For your kind reference, i have mentioned below the simple insert statement.

    <?php

    if($_POST['register'])
    {
    $name = $_POST['firstname'];
    $contA = $_POST['contadd'];
    $contNo = $_POST['contNo'];
    $email = $_POST['email'];
    $sub = $_POST['subject'];
    $comments = $_POST['comment'];
    $date = date("Y-m-d");
    $client_ip = $_SERVER['REMOTE_ADDR'];

    $qry = "Insert into feedback(fldName,fldContAddress,fldContNo,fldEmail,fldSubject,fldComment,fldDate,ip) values('$name','$contA','$contNo','$email','$sub','$comments','$date','$client_ip')";

    echo $qry;
    mysql_query($qry) or die("Cannot insert the records, please try again");

    echo "<script type='text/javascript'>";
    //echo "alert('Your feedback is send successfuly')";
    echo "location.href = ('confirmmessage.php')";
    echo "</script>";
    //}
    }
    ?>

    please kindly help to me.
     
    Ngajel, Jul 30, 2010 IP
  5. mahdi_fci3

    mahdi_fci3 Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think because you are using a local, but if you use this code in your real web site you will see the IP correctly, I tried this function before and I seen the same problem but when I used in my web site I get the IP correctly, if you need also to get the country and city of the user from the IP this following function will give you:
    
    	 
    function countryCityFromIP($ipAddr)
    {
       //function to find country and city name from IP address
       //Developed by Roshan Bhattarai 
       //Visit http://roshanbh.com.np for this script and more.
      
      //verify the IP address for the  
      ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
      // This notice MUST stay intact for legal use
      $ipDetail=array(); //initialize a blank array
      //get the XML result from hostip.info
      $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
      //get the city name inside the node <gml:name> and </gml:name>
      preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);
      //assing the city name to the array
      $ipDetail['city']=$match[2]; 
      //get the country name inside the node <countryName> and </countryName>
      preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);
      //assign the country name to the $ipDetail array 
      $ipDetail['country']=$matches[1];
      //get the country name inside the node <countryName> and </countryName>
      preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
      $ipDetail['country_code']=$cc_match[1]; //assing the country code to array
      //return the array containing city, country and country code
      return $ipDetail;
    } 
      
    
    
    
    $IPDetail=countryCityFromIP($_SERVER['REMOTE_ADDR']); 
    echo $IPDetail['country']; //country of that IP address 
    echo $IPDetail['city']; //outputs the IP detail of the city
    
    
    PHP:
     
    mahdi_fci3, Jul 31, 2010 IP