Storing an IP in a Database

Discussion in 'PHP' started by Jako, Jul 28, 2006.

  1. #1
    I have this much,
    $ip = $_SERVER['REMOTE_ADDR'];
    PHP:
    But how do I store the IP of someone who submits a form and keep it in the database?
     
    Jako, Jul 28, 2006 IP
  2. rjb

    rjb Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The same as with any variable... I can post a code snippet using PEAR DB if you would like me to.
     
    rjb, Jul 28, 2006 IP
  3. Jako

    Jako Well-Known Member

    Messages:
    347
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Yes, but I really don't know how to store a variable. I'm not good at PHP, just editing something that was already coded for me.

    Please post the example, thanks.
     
    Jako, Jul 28, 2006 IP
  4. rjb

    rjb Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here you go... hope this helps.

    
    require_once ("DB.php");
    
    $ip = $_SERVER['REMOTE_ADDR'];
    $vals = array("field_name", $ip)
    // Change field_name to what is in your table
    
    $DATABASE = "db_name";
    $USER = "user_name";
    $PASSWORD = "pass";
    $SERVER = "host_name";
    
    $dsn = mysql://$USER:$PASSWORD@$SERVER/$DATABASE";
    
    $dbconn =& DB::Connect($dsn, array());
    
    $prepare = $dbconn->prepare("INSERT INTO table_name VALUES (?, ?)");
    $result = $dbconn->execute($prepare, $vals);
    
    $dbconn->disconnect();
    
    PHP:
    I use the DB class so if you do not have this installed I am sorry... I ahve not tested this so maybe soneone could check my coding, but I beleive I have it about right.
     
    rjb, Jul 28, 2006 IP
  5. rjb

    rjb Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry... Line 12 should be:

    
    $dsn = "mysql://$USER:$PASSWORD@$SERVER/$DATABASE";
    
    PHP:
     
    rjb, Jul 28, 2006 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    What are yo trying to accomplish with the script?

    Are you looking to track visitors, or just keep a lost of IP addresses that visit your site?

    Here is a very light non-PEAR version of the same script. I know that some servers dont have PEAR installed by default.

    
    //Mysql Variables
    $host = 'localhost';
    $user = '';
    $pass = '';
    $db = 'ipTrack';
    $table = 'records';
    
    //connect to DB
    mysql_connect($host,$user,$pass) or die(mysql_error());
    
    //get remote IP
    $ip = $_SERVER['REMOTE_ADDR'];
    
    mysql_query("INSERT INTO $table VALUES ('', '$ip', NOW())");
    
    PHP:
    Here's a sql script for creating the table in the database:
    
    CREATE TABLE records (
    id int(11) NOT NULL auto_increment,
    ip varchar(20) NOT NULL default '',
    date datetime,
    PRIMARY KEY (id)
    ) TYPE=MyISAM; 
    
    Code (markup):
     
    jestep, Jul 28, 2006 IP