1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Question about IP tracking and storing of visitors

Discussion in 'PHP & Perl' started by OTrap, Dec 6, 2014.

  1. OTrap

    OTrap Jr. VIP Jr. VIP

    Joined:
    Jul 12, 2008
    Messages:
    2,285
    Likes Received:
    1,051
    I'm trying to set up a script 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 as the PHP on the page:

    Code:
    <?php
    $username = "[B]mysql_username[/B]";
    $password = "[B]password[/B]";
    $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([B]mysql_database[/B], $dbhandle) or die("Cannot find the database");
    
    $result = mysql_query("SELECT NULL FROM [B]table[/B] WHERE [B]column[/B]='$short'");
    
    if(mysql_num_rows($result)) {
    
    echo "Baha! You've been here before!";
    
    } else {
    
    mysql_query("INSERT INTO [B]table[/B] ([B]column[/B]) VALUES (" . $short . ")");
    echo "$ip is now $short";
    mysql_close($dbhandle);
    
    }
    
    ?>
    
    
    
    I currently have a database set up with a single column labeled "ip". However, what it places in that column upon a visitor doesn't match what the page echoes as $short. I assume that has something to do with it.
     
  2. MrBlue

    MrBlue Senior Member

    Joined:
    Dec 18, 2009
    Messages:
    950
    Likes Received:
    662
    Occupation:
    Web/Bot Developer
    Can you paste your table's schema here? The format of your IP column may be the issue.
     
    • Thanks Thanks x 1
  3. OTrap

    OTrap Jr. VIP Jr. VIP

    Joined:
    Jul 12, 2008
    Messages:
    2,285
    Likes Received:
    1,051
    Just realized I had the table's column set to INT(11). Changed it to VARCHAR(46). All seems to be working now.

    Your post drew my attention to it. Much appreciated!