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 9 of 9

Thread: Logging IPs

  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Logging IPs

    Hey guys,

    Could someone point me to a tutorial or help me with some snippets, what im trying to do is that when someone accesses a page it logs the last 30 ips used for that ID

    index.php?ID=edwgwegewg

    So i have a table which has a ID colum and a IP colum but how would i insert data into the specific row as well as keep 30 ips?

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So i am assuming your table is like this:

    [key][id ][ ip ]
    [ 0 ][id1][127.0.0.1]
    [ 1 ][id2][127.0.0.1]
    [ 3 ][id3][127.0.0.1]

    PHP Code:
    // untested code

    $sql "SELECT ip FROM table WHERE key = '".$key."'";

    $result mysql_query($sql
    or die(
    mysql_error());  

    if (!
    $result) {
       
    $message  'Invalid query: ' mysql_error() . "\n";
       
    $message .= 'Whole query: ' $query;
       die(
    $message);
    }

    $row mysql_fetch_row($result);
    $pos strrpos($row[0], ",");
    if (
    $pos === false) { 
        
    $iparr = array();
        
    $iparr[0] = $row[0];
    } else {
        
    $iparr split(','$row[0]);
    }
    $n count($iparr);
    array_reverse($iparr);
    array_push($iparr$_SERVER['REMOTE_ADDR']);
    array_reverse($iparr);
    $storeip = array();
    foreach(
    $iparr as $key => $value) {
        if(
    $key 30) {
            
    $storeip[$key] = $value;
        }
    }
    $store implode(","$storeip);

    $sql2 "UPDATE table SET ip='".$store."' WHERE key = '".$key."'";

    $result2 mysql_query($sql2
    or die(
    mysql_error()); 
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey thanks for that, im trying to read the code but if you could you explain the code used that would be great

    also the table is currently

    [ID][STATUS][IP]

    if i wanted to use your code i would have to add in a key colum?

  • #4
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by semaja2
    Hey thanks for that, im trying to read the code but if you could you explain the code used that would be great

    also the table is currently

    [ID][STATUS][IP]

    if i wanted to use your code i would have to add in a key colum?
    no key is the row number.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #5
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,631
    Thanks
    4
    Thanked 147 Times in 138 Posts
    Quote Originally Posted by semaja2
    Hey thanks for that, im trying to read the code but if you could you explain the code used that would be great

    also the table is currently

    [ID][STATUS][IP]

    if i wanted to use your code i would have to add in a key colum?
    What is your ID column? Is it an auto increment column so you avoid duplicates? If it isn't then you don't have a primary key for your table. if ID merely represents the user ID then you have nothing to distinguish between duplicate rows.

    also with an auto increment field, since you won't reuse the number you have something that will determine the previous rows so you could find the 30 previous for each user.

    also I would suggest you don't delete rows, merely keep adding them and then only select the previous 30 for each user. that way you would have a historical record of pages viewed that are older than the previous 30 for each user.

  • #6
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by guelphdad
    What is your ID column? Is it an auto increment column so you avoid duplicates? If it isn't then you don't have a primary key for your table. if ID merely represents the user ID then you have nothing to distinguish between duplicate rows.

    also with an auto increment field, since you won't reuse the number you have something that will determine the previous rows so you could find the 30 previous for each user.

    also I would suggest you don't delete rows, merely keep adding them and then only select the previous 30 for each user. that way you would have a historical record of pages viewed that are older than the previous 30 for each user.
    my code will

    in a table cell store all 30 records as comma seperated values.

    This is simple to extract, and easy to work with and update.

    the table col will have to be long but, meh..

    what the codes does is , selects said cell. breaks it into an array (a, b, c, d)
    reverses the array (d, c, b, a)
    add to the end (d, c, b, a, X)
    reverse the array again (X, a, b, c, d)
    then grab the top 30 and store them comma seperated. so new values will always appear at the front of the string.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #7
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks guys for all the help, i know i must seem like a big idiot in alot of cases but when i run the code i get this error:

    Status: Script: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key = ''' at line 1

    PHP Code:
    <?php

    $id 
    $HTTP_GET_VARS['id'];



    $dbh=mysql_connect ("localhost""semaja2_tracker""*****") or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("semaja2_tracker");

    $sql "SELECT * FROM machines1 WHERE id='$id'";

    $result mysql_query($sql);

    $row mysql_fetch_array($result) ;

    ?>

        Status: <? echo $row['status']; ?>
        Script: <? echo $row['script']; ?>
        
    <?
    // untested code 

    $sql "SELECT ip FROM machines1 WHERE key = '".$key."'"

    $result mysql_query($sql)  
    or die(
    mysql_error());   

    if (!
    $result) { 
       
    $message  'Invalid query: ' mysql_error() . "\n"
       
    $message .= 'Whole query: ' $query
       die(
    $message); 


    $row mysql_fetch_row($result); 
    $pos strrpos($row[0], ","); 
    if (
    $pos === false) {  
        
    $iparr = array(); 
        
    $iparr[0] = $row[0]; 
    } else { 
        
    $iparr split(','$row[0]); 

    $n count($iparr); 
    array_reverse($iparr); 
    array_push($iparr$_SERVER['REMOTE_ADDR']); 
    array_reverse($iparr); 
    $storeip = array(); 
    foreach(
    $iparr as $key => $value) { 
        if(
    $key 30) { 
            
    $storeip[$key] = $value
        } 

    $store implode(","$storeip); 

    $sql2 "UPDATE table SET ip='".$store."' WHERE key = '".$key."'"

    $result2 mysql_query($sql2)  
    or die(
    mysql_error());  

    ?>

  • #8
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,631
    Thanks
    4
    Thanked 147 Times in 138 Posts
    Quote Originally Posted by rlemon
    my code will

    in a table cell store all 30 records as comma seperated values.

    This is simple to extract, and easy to work with and update.
    That is poor database design, you should not store multiple values in a single cell.

    Since you have your values in a string, please show me how you would code any of the following:

    1) show me the total number of users that landed on any 3 of the following 5 pages:

    (blue, green, yellow, orange, purple).

    2) of the users that landed on yellow, show how many times they have landed on purple within their last 30 pages?

    your queries will be much more complicated and take longer to process than if you normalize your data with a single item in each row.

  • #9
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by semaja2
    thanks guys for all the help, i know i must seem like a big idiot in alot of cases but when i run the code i get this error:

    Status: Script: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key = ''' at line 1

    PHP Code:
    <?php

    $id 
    $HTTP_GET_VARS['id'];



    $dbh=mysql_connect ("localhost""semaja2_tracker""*****") or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("semaja2_tracker");

    $sql "SELECT * FROM machines1 WHERE id='$id'";

    $result mysql_query($sql);

    $row mysql_fetch_array($result) ;

    ?>

        Status: <? echo $row['status']; ?>
        Script: <? echo $row['script']; ?>
        
    <?
    // untested code 

    $sql "SELECT ip FROM machines1 WHERE key = '".$key."'"

    $result mysql_query($sql)  
    or die(
    mysql_error());   

    if (!
    $result) { 
       
    $message  'Invalid query: ' mysql_error() . "\n"
       
    $message .= 'Whole query: ' $query
       die(
    $message); 


    $row mysql_fetch_row($result); 
    $pos strrpos($row[0], ","); 
    if (
    $pos === false) {  
        
    $iparr = array(); 
        
    $iparr[0] = $row[0]; 
    } else { 
        
    $iparr split(','$row[0]); 

    $n count($iparr); 
    array_reverse($iparr); 
    array_push($iparr$_SERVER['REMOTE_ADDR']); 
    array_reverse($iparr); 
    $storeip = array(); 
    foreach(
    $iparr as $key => $value) { 
        if(
    $key 30) { 
            
    $storeip[$key] = $value
        } 

    $store implode(","$storeip); 

    $sql2 "UPDATE table SET ip='".$store."' WHERE key = '".$key."'"

    $result2 mysql_query($sql2)  
    or die(
    mysql_error());  

    ?>

    again, key is representative of a key identifier for the table row. i don't know your table design. looks like you use an id value?
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<


  •  

    Posting Permissions

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