my ip log script is bugged.. can anyone find the problem? :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

my ip log script is bugged.. can anyone find the problem? :(

Post by Coco »

if ($temp = getenv("REMOTE_ADDR"))
{
$ipaddress = $temp;
}
elseif ($temp = $HTTP_SERVER_VARS['REMOTE_ADDR'])
{
$ipaddress = $temp;
}
else
$ipaddress = '0';

if($ipaddress!='0')
{
if($ipaddress==mysql_result($res,0,"balance"))
{
//ip hasnt change, do nothing
}
else
{
//ip changed.. check for same in db
$query1 = "SELECT id, nick, balance FROM users WHERE balance = '". $ipaddress . "' AND nick != '" . $username . "'";
$result1 = mysql_query($query1);
if(mysql_num_rows($result1)>0)
{
//ip found. check for existing link
$now = date(Y) . date(m) . date(d) . date(H) . date(i) . date(s);
$query2 = "SELECT linkid FROM links WHERE userid = '" . $LOGGED_IN . "'";
$result2 = mysql_query($query2);
if(mysql_num_rows($result2)>0)
{
//link exists
}
else
{
//user not in link. try find link for other username
$newlink = md5(uniqid(rand()));
$query3 = "INSERT INTO links VALUES ('" . $LOGGED_IN_USERNAME . "', '" . $LOGGED_IN . "', '" . $newlink . "', '" . $now . "')";
$result3 = mysql_query($query3);
while($data1 = mysql_fetch_array($result1))
{
//check if in existing link
$query5 = "SELECT linkid FROM links WHERE userid = '" . $data1['id'] . "'";
$result5 = mysql_query($query5);
if(mysql_num_rows($result5)>0)
{
//2nd user is in a link. check link != new link, and if != then re-id old link
if(mysql_result($result5,0,"linkid")!=$newlink)
{
$query6 = "UPDATE links SET linkid = '" . $newlink . "' WHERE linkid = '" . mysql_result($result5,0,"linkid") . "'";
$result6 = mysql_query($query6);
}
else
{
//already in link... do nothing
}
}
else
{
//not in a link, add to new link
$query3 = "INSERT INTO links VALUES ('" . $data1['nick'] . "', '" . $data1['id'] . "', '" . $newlink . "', '" . $now . "')";
$result3 = mysql_query($query3);
}
}
}
}
$query4 = "UPDATE users SET reg_date = reg_date, balance = '" . $ipaddress . "' WHERE id = '" . $LOGGED_IN . "'";
$result4 = mysql_query($query4);
}
Heres how it works. When you login, the script checks your ip address against the recorded ip. If it changed, then i want to check it against the ip address used by everyone else in the db, to create a link (users are only supposed to have 1 account)
If your account is already linked, nothing happens. If it isnt, and theres a second user with your ip address, the system creates a link record (1 line per user, all links have a uniqid, linked users the same linkid.) If the second (or 3rd etc) user is part of a link, then that link is added to the new link.

Problem is, roughly 1 time in 10, the system adds a link to the database, but only containing 1 user, when the user has no matching ip addresses in the system (i checked)
I cant find the reason why :(
suggestions? obvious errors?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I didn't understand the script but extensive use of http://www.php.net/manual/en/function.error-log.php in all states of the script may help.
Post Reply