...

View Full Version : Logging IPs



semaja2
06-20-2006, 08:55 AM
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?

rlemon
06-20-2006, 04:08 PM
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]



// 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());

semaja2
06-20-2006, 11:09 PM
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?

rlemon
06-21-2006, 02:18 PM
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.

guelphdad
06-21-2006, 02:35 PM
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.

rlemon
06-21-2006, 02:39 PM
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.

semaja2
06-21-2006, 02:46 PM
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

$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());

?>

guelphdad
06-21-2006, 03:53 PM
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.

rlemon
06-21-2006, 07:02 PM
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

$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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum