Archive

Any PHP/SQL gurus here?

  • O-Trap
    I'm trying to set up a script (Where's Ender when you need him?) that checks a visitor's IP address again a database. If it's not already in the database, the script adds it. If it is already in there, it does something else.

    I had this sometime before, but I don't remember how the database was set up. Here's what I had:

    [PHP]
    <?php
    $username = "mysql_username";
    $password = "password";
    $hostname = "localhost";
    $ip = $_SERVER['REMOTE_ADDR'];
    $short = ip2long($ip);


    $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
    $dbname = mysql_select_db(mysql_database, $dbhandle) or die("Cannot find the database");

    $result = mysql_query("SELECT NULL FROM table WHERE column='$short'");

    if(mysql_num_rows($result)) {

    echo "Baha! You've been here before!";

    } else {

    mysql_query("INSERT INTO table (column) VALUES (" . $short . ")");
    echo "$ip is now $short";
    mysql_close($dbhandle);

    }

    ?>


    [/PHP]

    Anyone know how the table should be setup?
  • ernest_t_bass
    I don't have any experience whatsoever.

    Hope this helps.
  • Jawbreaker
    I haven't done much PHP/MySQL in the past year but this should work:

    table_ip_address_log
    ip_address_log_id, int(10), NO NULL, Primary Key, Auto Increment
    ip_address_log_datetime, datetime
    ip_address, varchar(46)

    I would take a look and see if you want to use ip2long since it only deals with IPv4 and doesn't handle IPv6 (I think).
  • O-Trap
    Damn. Had the SQL column set to INT(11). Changed it to VARCHAR(46) and it's working well now.

    Actually thought to look because of your post, Jawbreaker. Much appreciated. Reps!