PHP & MYSQL ip address check

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Boujii
    New Member
    • Apr 2007
    • 5

    PHP & MYSQL ip address check

    Code:
    <html> 
    <head> 
    <title>Add New MySQL User</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    </head> 
    <body> 
    <? 
    if(isset($_POST['add'])) 
    { 
    
    include 'c:\sokkit\config.php'; 
    include 'c:\sokkit\opendb.php'; 
    $ip=$_SERVER['REMOTE_ADDR'];
    $username = $_POST['username']; 
    $password = $_POST['password'];
    $country = $_POST['mydropdown'];
    
    $query = "INSERT INTO account (username, password, location, forumip) VALUES ('$username', '$password', '$country', '$ip')"; 
    mysql_query($query) or die('Error, insert query failed'); 
    
    $query = "FLUSH PRIVILEGES"; 
    mysql_query($query) or die('Error, insert query failed'); 
    
    include 'c:\sokkit\closedb.php'; 
    echo "New MySQL user added"; 
    } 
    else 
    { 
    ?> 
    <form method="post"> 
    <div align="left">
    <select name="mydropdown">
    <option value="Zimbabwe">Zimbabwe</option>
    </select>
    </div>
    <table width="400" border="0" cellspacing="1" cellpadding="2"> 
    <tr>  
    <td width="100">Username</td> 
    <td><input name="username" type="text" id="username"></td> 
    </tr> 
    <tr>  
    <td width="100">Password</td> 
    <td><input name="password" type="text" id="password"></td> 
    </tr> 
    <tr>  
    <td width="100">&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr>  
    <td width="100">&nbsp;</td> 
    <td><input name="add" type="submit" id="add" value="Add New User"></td> 
    </tr> 
    </table> 
    </form> 
    <? 
    } 
    ?> 
    </body> 
    </html>
    this is an account creation I am trying to put together. Totally newbie in php and html i am no better, so please bare with me. So far everything works good. When I submit, it writes everything including the ip in mysql database. As you can see $ip is the variable for ip address. What I am looking to do is prevent anyone from doing multiple creates from same ip. I have learned how to write to the mysql database, but not learned yet how to read. I will need to read from the "account" table and "forumip" field to see if the ip exists.
    kinda like this:
    select * from `account` where forumip=$ip';

    So basically when someone presses submit, if ip in database already to error rather then submitting the info.
    Any help in this area is greatly appreciated.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    There is a very simple way of querying the database and counting the rows it returns. In your case, if it returns more than 0 rows you would not add the new user.
    Like this:
    [PHP]
    // Create the query and execute it
    $QUERY = "SELECT field FROM table WHERE field = value";
    $RESULT = mysql_query($QU ERY) or die(mysql_error ());

    // Check how many rows the query returned
    if(mysql_num_ro ws($RESULT) > 0) {
    echo "The value already exists";
    }
    else {
    // Put your insert code here.
    }
    [/PHP]

    This is very simple and will do fine in your case, because your design only allows for one row to be return.

    However, if you were to use this method to count, for example, every single row in your table, this would return a lot of data you dont need.
    In that kind of a situation I would rather reccomend an approach like this:
    [PHP]
    // Create query and execute
    $QUERY = "SELECT COUNT(field) AS 'count' FROM table WHERE field = value";
    $RESULT = mysql_query($QU ERY) or die(mysql_error ());

    // Read the firs row
    $row = mysql_fetch_ass oc($RESULT);

    // Check how many rows MySQL counted
    if($row['count'] > 0) {
    echo "value already exists";
    }
    else {
    // Put input code here
    }
    [/PHP]

    This way MySQL only returns one row no matter how many it counts, which will lighten the load on the server on a busy day, especially if the MySQL server is on another server.

    Hope this helps.

    Comment

    • Boujii
      New Member
      • Apr 2007
      • 5

      #3
      Thanks Atli. That titbit of code worked great. :)

      Comment

      Working...