PHP Arrays (in_array)

Soldato
Joined
27 Dec 2005
Posts
17,316
Location
Bristol
I'm trying to do an in_array on a database array. Code:

Code:
$sqlips = mysql_query("SELECT * FROM blockedips");
$ips = mysql_fetch_array($sqlips);
if (in_array ($_SERVER['REMOTE_ADDR'], $ips)) {
exit();
} else {
......

and I'm getting the error;

Warning: in_array() [function.in-array]: Wrong datatype for second argument in xxxxxx/log.php on line 4

I've tried using the in_array straight on $sqlips as well and get the same error. Any ideas?
 
Are you sure it works?

mysql_fetch_array() only returns one row not the entire set of results.

Have a read through this tutorial. It gives examples of the output mysql_fetch_array() will produce along with code for using it.
http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php

rghjones said:
thought it would just pass through but turns out it returns an error!
When there are no results returned by the query mysql_fetch_array() returns FALSE. You'll need to check for this condition.

Code:
if ($ips = mysql_fetch_array($sqlips)) {
  //we have a result do some processing
} else {
  //no results print an error or something
}
(In most cases you will be using a while loop rather than an if statement)
 
I think/hope so, I guess there's no way of me checking as I'm not a blocked IP.

Maybe you could advise an alternative piece of code? Basically I have a log system which logs visitors to my site and the order in which they view pages. Through this I've now created the option to delete all records with a certain IP as well as blocking that IP from being logged by the tracker (but not viewing the site).

So when I delete a row, it also adds the IP address to a db named blockedips. My log file then does the above before processing a visitor and entering them into the log db. So basically I've got a db with IP's and their unique ID's which I want to try and match to the users IP address. If there's a match to any one of the records, then script won't run.
 
I guess there's no way of me checking as I'm not a blocked IP.

you have control of the database? add you ip to it my man

edit: didn't read fully what you doing but a better way to do it would be to query your blockedisp table for the current users ip, adding LIMIT 1 to the end of the query string, and do mysql_num_rows. if more then 0 rows means the users ip has been blocked, saves having to return the whole database as it will stop after one is found and only return true or false.

don't quote me on this as i have only skimmed stuff about ip and blocking them, but afaik peoples ip change all the time so blocking one is not a good idea as you may block a bad user but then a good user is given that ip and then they are blocked, and then the block only works for as long as the ip is the bad users ip address.

one other method would be to use cookies. obviously they can easily be got around but if the user has no clue what they are doing then you can set a cookie when you block the user, then when the revisit the cookie will still be there and will stop them from doing whatever you want to stop them doing.
 
Last edited:
Good idea with the num_rows, works a treat and like you says saves querying the entire database (even though it's not that big).

I'm not too worried about bad ip's becoming good ip's as there's no difference to what the end user can see or do, it simply limits what traffic gets logged by a visitor tracker I've made. It's pretty easy to tell what are bots and what are not (ie. a visitor that can't store sessions and visits a different page every 2 seconds). Was getting annoying filtering through all these to see the real people so now I've added a function which deletes all entries with a specific IP and also adds that IP it to the blocked db.
 
Usage example of mysql_fetch_array..
Code:
while ($foo= mysql_fetch_array($resource)) {
  // do something with $foo, which is an array of *columns* not rows
}

mysql_num_rows() will still need you to query the DB just the same as before, but instead of fetching rows, you'll just be counting them.

Use a better select statement instead..
Code:
$result = mysql_query('SELECT count(*) AS c FROM `table`');
$row = mysql_fetch_assoc($result);
if ($row['c']) die("Begone foul banned person!");
 
Last edited:
Similar to what I'm using now... but more understandable to me :p

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

$sqlips = mysql_query("SELECT * FROM blockedips WHERE ip='$ip'");
$checkips = mysql_num_rows($sqlips);
if($checkips < 1){
...... // log etc
 
Back
Top Bottom