Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Location
    Florida
    Posts
    155
    Thanks
    16
    Thanked 2 Times in 2 Posts

    Saving IP address in database.

    Hello there,

    what I have been searching for on google, here, and daniweb is a way to save the registration form submitter's IP address. This is to prevent spamming of accounts. Here is the registration form itself.
    Code:
    <?php include("tophead.php"); ?>
    <?php include("header.php"); ?>
    <form name="registration_form" method="post" action="register.php">
    Username: <input type="text" name="username"><br />
    Password: <input type="password" name="password"><br />
    Email: <input type="text" name="email">(Enter only if you want to receive news/deals/and other stuff)<br />
    <input id="ip" name="ip"  value='<?php echo $_server["remote_address"]; ?>' type='hidden'>
    <input type="submit" value="Submit">
    </form>
    </p>
    </body>
    </html>
    Here is register.php
    PHP Code:
    <?php
     $to 
    "*****";
     
    $subject "New Member";
     
    $body "A new member has joined.";
     if (
    mail($to$subject$body)) {
       echo(
    "<p>Thank you for joining! You may now login!</p>");
      } else {
       echo(
    "<p>A problem has occur, please try again.</p>");
      }
    ?>
    <?php
    $connect
    =mysql_connect("localhost","******",
        
    "*****");
    mysql_select_db("****",$connect) or 
       die (
    mysql_errno().":<b> ".mysql_error()."</b>");
    $insert_query 'insert into     user (
                        
                        username,
                        password,
                        email,
                        ip
                        ) 
                        values
                        (
                        
                        "' 
    $_POST['username'] . '",
                        "' 
    md5($_POST['password']) . '",
                        "' 
    $_POST['email'] . '",
                        "' 
    $_POST['ip'] . '"
                        )'
    ;

    mysql_query($insert_query);
    ?>
    <meta http-equiv="refresh" content="3;url=index.php"/>
    In the MySQL db, "ip" is saved as int(20).

    Thanks for the info!

  • #2
    New Coder
    Join Date
    Jan 2010
    Location
    UT
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    An IP address isn't a number, its a string due to the decimal points. Change your database field that stores the IP address to varchar(15) instead.

    Also, the correct PHP code to get someone's IP address is:

    $_SERVER["REMOTE_ADDR"]

    You have "remote_address".

    I also suggest not making the IP a hidden field in your form, but get the value of it after the form is submitted.

    If you need anymore help I'll be glad to assist.



    Edit: you want to use mysql_real_escape_string() on the variables that are in your query to help prevent sql injection too

  • Users who have thanked Zoic for this post:

    The reaper (06-18-2010)

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Location
    Florida
    Posts
    155
    Thanks
    16
    Thanked 2 Times in 2 Posts
    thank you very much! I am going to change remote part and read more about prevention injections.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    There are numerous benefits gained when storing IPs as integers:
    http://bafford.com/2009/03/09/mysql-...-ip-addresses/

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Location
    Florida
    Posts
    155
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Ok, so now I am confused...do I save it as varchar or int? Which has the better benefits to use?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •