Unstoppable Domains

IP logging script

Spaceship Spaceship
Watch

SiKing

Registered MemberEstablished Member
Impact
6
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.

Here's my code

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

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.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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
 
0
•••
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;
 
0
•••
yeah...lol

Okay then, replace this

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

with:

$views_array = explode($views,',');
$views_array[0] = "";
 
0
•••
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
 
0
•••
$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 == "") {
$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'");
 
0
•••
That's solved the comma problem but the list is still being overwritten
 
0
•••
Keep in mind,
explode($views,',')
should be
explode(',',$views)
 
0
•••
Oh yeah, Whoops. Thanks for pointing that out. Problem with overwriting still remains though. :(
 
0
•••
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.
 
0
•••
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.
 
Last edited:
0
•••
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:
$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);

$query = mysql_query("UPDATE bp_events SET views='".implode(",", $views_array)."' WHERE id = '$id'");
 
0
•••
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
 
0
•••
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.
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back