Adding an IP address to a contact form

Discussion in 'PHP' started by xtreme fever, Apr 3, 2006.

  1. #1
    Hello,

    I have a contact form but, when my users submit my form, I need the form to include their IP address. How do I do this with the php script I am using?

    Thanks
     
    xtreme fever, Apr 3, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In the form you need the following hidden variable:
    <?php
    $ipaddress = GetHostByName($REMOTE_ADDR);
    ?>
    <input type="hidden" name="ipaddress" value="<?php echo $ipaddress ?>">
    PHP:
    And then in the processing side you just use the $ipaddress variable in the same way you use the other variables.
     
    mad4, Apr 3, 2006 IP
  3. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i'd suggest you use the $_SERVER['REMOTE_ADDR'] variable instead...
     
    daboss, Apr 3, 2006 IP
  4. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #4
    i suggest to attach this ip adress right before you call the the mail() function to the end of your mailtext-var. it dosnt needs much to override this hidden field and insert whatever you want at this place.
     
    falcondriver, Apr 3, 2006 IP
  5. xtreme fever

    xtreme fever Active Member

    Messages:
    472
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    95
    #5
    Thanks guys
     
    xtreme fever, Apr 3, 2006 IP
  6. themole

    themole Peon

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The only problem with the hidden field is that I could change that value to whatever I wanted. Any POST or GET field can be changed by the user. *NEVER* trust any user input. EVER.

    You need to grab their IP after the form has been submitted - so you get the submitters IP.

    For example:

    
    <?php
    
    if($_POST['send_mail'])
    {
    	//get your other fields here
    	
    	$ip = $_SERVER['REMOTE_ADDR'];
    	
    	$email_body .= "\n\n$ip";
    	
    	//mail();
    	
    	echo "Thanks for sending me an email!";
    }
    
    else
    {
    ?>
    
    <!-- your mail form here -->
    
    <?php
    }
    ?>
    
    Code (markup):
    -the mole
     
    themole, Apr 3, 2006 IP
  7. hdpinn

    hdpinn Peon

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Here's what I use, it's a bit more bulletproof to return an IP in all cases...

    $user_ip = $_SERVER['REMOTE_ADDR'];
    if (trim($user_ip) == '') {
    	$user_ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    if (trim($user_ip) == '') {
    	$user_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    PHP:
     
    hdpinn, Apr 7, 2006 IP
  8. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #8
    both your if statements are the same. So whats the diffrence?
     
    asgsoft, Apr 7, 2006 IP
  9. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #9
    Not the same. hdpinn checks for real ip first then proxy ip if the first does not exist and so on.
     
    Lordo, Apr 8, 2006 IP
  10. webbist

    webbist Peon

    Messages:
    89
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Please note, $REMOTE_ADDR only works when register_globals under php.ini is turned on (it is turned off on default installations). I'd recommend going with the correct way, $_SERVER['REMOTE_ADDR']

    Just my two cents.
     
    webbist, Apr 9, 2006 IP
  11. SecondV

    SecondV Active Member

    Messages:
    76
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    70
    #11
    Something I use:
    
    $ip = (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
    
    PHP:
    @webbist only turned off by default in PHP versions 4.2.0 and later. Also, many webhosts turn it ON. And most folks do not have access to php.ini. While on the subject of register_globals, here's a way to turn it off using .htaccess:
    
    php_flag register_globals 0
    
    Code (markup):
    But note, on some servers it may cause a 500 internal server error.
     
    SecondV, Apr 12, 2006 IP
  12. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #12

    When I use it it doesn't work. I have it on http://thewebmasterstool.com/'s poll and it lets me vote more than once.

    How can I solve that?
     
    asgsoft, May 3, 2006 IP
  13. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #13
    1. Try to reverse it (look for http_x_... before remote_dir)
    2. Let us see the code you use to prevent same IP voting.
     
    Lordo, May 3, 2006 IP
  14. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #14
    This is the script I am using:

    
    <?php
    include 'config.php';
    mysql_connect("$host", "$user", "$pass") or die(mysql_error()); 
    mysql_select_db("$dbname") or die(mysql_error());
    $ip = $_SERVER['REMOTE_ADDR'];
    if (trim($ip) == '') {
    	$ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    if (trim($ip) == '') {
    	$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    
    $query = mysql_query("select * from vote where ip='$ip'");
    $countbans = mysql_num_rows($query);
    if($countbans > 0) {
    include "results.php";
    echo("You have Already Voted!");
    }else{
    
    $action = $_GET['action'];
    if(!isset($action) || empty($action) || $action !== "addvote"){
    echo("<form name='form1' method='post' action='?action=addvote'>
                        <table width='100%' border='0' cellspacing='2' cellpadding='0'>
                          <tr> 
                            <td colspan='2'><div align='left'>Do you like my site?</div></td>
                          </tr>
                          <tr> 
                            <td width='19%'><input type='radio' name='vote' value='ex'></td>
                            <td width='81%' bgcolor='#F4F4F4'>It's Excellent!</td>
                          </tr>
                          <tr> 
                            <td><input type='radio' name='vote' value='co'></td>
                            <td bgcolor='#F4F4F4'>Pretty Cool</td>
                          </tr>
                          <tr> 
                            <td><input type='radio' name='vote' value='ok'></td>
                            <td bgcolor='#F4F4F4'>OK</td>
                          </tr>
                          <tr> 
                            <td><input type='radio' name='vote' value='ha'></td>
                            <td bgcolor='#F4F4F4'>Hate It!</td>
                          </tr>
                          <tr> 
                            <td></td>
                            <td><input type='submit' name='Submit' value='Vote!'></td>
                          </tr>
                        </table>
                      </form>");
    }elseif($action == "addvote"){
    //show result
    echo "<p><h4>Thank you For Voting</h4></p>";
    $vote = $_POST['vote'];
    include 'config.php';
    $ip = $_SERVER['REMOTE_ADDR'];
    if (trim($ip) == '') {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    if (trim($ip) == '') {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } 
    mysql_connect("$host", "$user", "$pass") or die(mysql_error()); 
    mysql_select_db("$dbname") or die(mysql_error());
    mysql_query("INSERT INTO `vote` (`vote` , `ip` ) VALUES ('$vote', '$ip')");
    }
    }
    ?>
    
    PHP:
     
    asgsoft, May 3, 2006 IP
  15. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #15
    Try this:
    Remove:
    if (trim($ip) == '')
    {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    if (trim($ip) == '')
    {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
     
    Lordo, May 3, 2006 IP
  16. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #16
    you didn't mention what I need to change it with?
     
    asgsoft, May 4, 2006 IP