NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page IP logging script

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 12-17-2006, 06:58 AM THREAD STARTER               #1 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




Help! IP logging script


Hey. Need a little help please. Basically I have a script which takes a list of IPs out of a MySQL database and if the user's IP is not in the array, then it adds it to the list and updates the database. However, it keeps adding a comma as a value in the array somewhere in the process of exploding and imploding the IPs.
????: NamePros.com http://www.namepros.com/programming/270144-ip-logging-script.html

Here's my code

PHP Code:
$ip $_SERVER['REMOTE_ADDR'];

$query mysql_query("SELECT * FROM events WHERE id='$id'");
while (
$row mysql_fetch_array($query)) {
$views stripslashes($row['views']);
????: NamePros.com http://www.namepros.com/showthread.php?t=270144
}


$views_array = array();

if (
$views == "") {        
            
$views_array[] = $ip;
            }
            
else {
$views_array explode($views,',');
    if (!
in_array($ip$views_array)) {
    
$views_array[] = $ip;
    }        
  }

$query mysql_query("UPDATE bp_events SET views='".implode(","$views_array)."' WHERE id = '$id'"); 
I've cleaned it up a bit for the sake of this post. Any help will be very much appreciated.
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 12-17-2006, 07:45 AM   #2 (permalink)
Senior Member
 
RegisterRants's Avatar
Join Date: Oct 2006
Location: NJ
Posts: 1,152
RegisterRants has a spectacular aura aboutRegisterRants has a spectacular aura aboutRegisterRants has a spectacular aura about
 




Replace this:

$views_array = explode($views,',');

With:

$views_array = explode($views,',');
$counterx = sizeof($counterx) - 1;
$views_array[$counterx] = "";

See if that works; it should remove the last value of $views_array
__________________
Web Development
RegisterRants is offline  
Old 12-17-2006, 07:56 AM THREAD STARTER               #3 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




Thanks for the suggestion. However, the comma is added to the beginning of the array each time. Sorry - I should have said.

Also, should:

$counterx = sizeof($counterx) - 1;

be:

$counterx = sizeof($views_array) - 1;
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 12-17-2006, 07:58 AM   #4 (permalink)
Senior Member
 
RegisterRants's Avatar
Join Date: Oct 2006
Location: NJ
Posts: 1,152
RegisterRants has a spectacular aura aboutRegisterRants has a spectacular aura aboutRegisterRants has a spectacular aura about
 




yeah...lol

Okay then, replace this

$views_array = explode($views,',');

with:

$views_array = explode($views,',');
$views_array[0] = "";
__________________
Web Development
RegisterRants is offline  
Old 12-17-2006, 08:10 AM THREAD STARTER               #5 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




Thanks again but it's still happening

I've also got another problem. Rather than adding the new IP to the list, it's completely overwriting the list. So every time the page is refreshed, the list looks like:

Code:
,XX.XXX.XXX.192
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 12-17-2006, 08:14 AM   #6 (permalink)
Senior Member
 
RegisterRants's Avatar
Join Date: Oct 2006
Location: NJ
Posts: 1,152
RegisterRants has a spectacular aura aboutRegisterRants has a spectacular aura aboutRegisterRants has a spectacular aura about
 




$ip = $_SERVER['REMOTE_ADDR'];

$query = mysql_query("SELECT * FROM events WHERE id='$id'");
while ($row = mysql_fetch_array($query)) {
$views = stripslashes($row['views']);
}


$views_array = array();
for ($count = 0; $count <= sizeof($views) - 1; $count++){
if ($views == "") {
????: NamePros.com http://www.namepros.com/showthread.php?t=270144
$views_array[$count] = $ip;
$views_array[$count] = trim($views_array[$count],",");
}

else {
$views_array = explode($views,',');
if (!in_array($ip, $views_array)) {
$views_array[$count] = $ip;
$views_array[$count] = trim($views_array[$count],",");
}
}
}

$query = mysql_query("UPDATE bp_events SET views='".implode(",", $views_array)."' WHERE id = '$id'");
__________________
Web Development
RegisterRants is offline  
Old 12-17-2006, 08:29 AM THREAD STARTER               #7 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




That's solved the comma problem but the list is still being overwritten
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 12-17-2006, 09:13 AM   #8 (permalink)
Senior Member
 
legend2's Avatar
Join Date: Sep 2005
Posts: 1,112
legend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud of
 



Keep in mind,
explode($views,',')
should be
explode(',',$views)
legend2 is offline  
Old 12-17-2006, 09:25 AM THREAD STARTER               #9 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




Oh yeah, Whoops. Thanks for pointing that out. Problem with overwriting still remains though.
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 12-17-2006, 09:31 AM   #10 (permalink)
Senior Member
 
legend2's Avatar
Join Date: Sep 2005
Posts: 1,112
legend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud of
 



miseria, your code is incorrect.

check the loop, $views is overwritten every time, so when you are out of the loop, $views contains the last value. so the $views_array will surely contain only one value. unless that is what you want it to be.

Also, why do you have 2 tables, views and bp_views? you read from the first and update int he second.
legend2 is offline  
Old 12-17-2006, 09:37 AM THREAD STARTER               #11 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




You mean events and bp_events? it is all one table. I just modified it to make it less confusing but it actually did the opposite. It is correct in my script though.

I am not sure where $views is being overwritten. (Sorry - it's been a bad day).

EDIT: I think you mean where I am retrieving it from the database. 'views' is a list of IP addresses separated by a comma. This is why it is exploded later on.
__________________
Encenta - Amazon Associates CMS
Last edited by Encenta.com; 12-17-2006 at 09:52 AM.
Encenta.com is offline  
Old 12-17-2006, 10:29 AM   #12 (permalink)
Senior Member
 
legend2's Avatar
Join Date: Sep 2005
Posts: 1,112
legend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud of
 



So there is only one row in the table with the ips separated by comas. So technically you don't need that while loop at the start of the code.

Secondly, I need to know what you exactly have. Execute this code and show me the output.

PHP Code:
$ip $_SERVER['REMOTE_ADDR'];

$query mysql_query("SELECT * FROM events WHERE id='$id'");
while (
$row mysql_fetch_array($query)) {
$views stripslashes($row['views']);
}

echo 
"views is: $views<br>";
$views_array = array();

if (
$views == "") {        
            
$views_array[] = $ip;
            }
            
else {
$views_array explode(',',$views);
    if (!
in_array($ip$views_array)) {
    
$views_array[] = $ip;
    }        
  }
echo 
"printing new array<br>";
print_r($views_array);
????: NamePros.com http://www.namepros.com/showthread.php?t=270144

$query mysql_query("UPDATE bp_events SET views='".implode(","$views_array)."' WHERE id = '$id'"); 
????: NamePros.com http://www.namepros.com/showthread.php?t=270144
legend2 is offline  
Old 12-17-2006, 12:26 PM THREAD STARTER               #13 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




http://beenplaces.com/2/test

Had to make a few modifications - connecting to database, changing database name and changing '$id' to an actual number.

Thanks
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 12-17-2006, 02:43 PM   #14 (permalink)
Senior Member
 
legend2's Avatar
Join Date: Sep 2005
Posts: 1,112
legend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud of
 



the array that you are imploding is apparently correct, more than one entry.
you need to check why the query is not updating the row.
legend2 is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 01:30 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger