ip blocking

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
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

ip blocking

Post by cheatboy00 »

o.k. what i'm trying to do is check a person ip address aginst a list of ips i already have. and if its there show the results page so they cants vote again, if their ip address isnt in the list then allow them to vote.

Code: Select all

// this is in teh index2a.php file
$ip = getenv('REMOTE_ADDR');

if (mysql_query("SELECT * FROM poll WHERE ip = $ip")){
   include("showresults.php");
} else {
   include("poll.html");
}



// a whole other page, the poll.php, the page when you submit your vote

$ip = getenv('REMOTE_ADDR');

mysql_query("INSERT INTO `poll` (`vote`, `ip`) VALUES ('$vote', '$ip')");

header("Location: http://dragonsblood.nicewebshost.com/cbaa/index2a.php");
please help... see what happens is even if your ips in the poll table under the ip colum it still shows the poll.html and doesnt switch to show results.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

you've got the same problem you had before. mysql_query only returns a handle to a query response. It returns true on successful queries, and false on unsuccessful ones; therefore, if your query goes through correctly, poll.html is always the result. You want:

Code: Select all

$result = mysql_query("SELECT * FROM poll WHERE ip=$ip
if (mysql_num_rows($result) >0)
    include 'showresults.php';
else
    include 'poll.html';
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

parse error

Code: Select all

$result = mysql_query("SELECT * FROM poll WHERE ip=$ip"); 
if (mysql_num_rows($result) >0) 
    include 'showresults.php'; 
else 
    include 'poll.html';
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

good call, sloppy programming on my part
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

wow o.k. i think i'm getting this, in mysql if somethign is going to return true its going to have a 1 value if faulse then a 0 right....
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

Poll

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/dragonsb/public_html/cbaa/footer.php on line 116

thats the error i'm getting with the statement you gave me.. then i tried this and it worked.

Code: Select all

$result = mysql_query("SELECT * FROM poll WHERE ip=$ip"); 
            
if ($result = 1){ 
   include 'showresults.php'; 
} else { 
    include 'poll.html'; 
}

thanks for all the help you guys are great
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

$result = mysql_query("SELECT * FROM poll WHERE ip=$ip"); 
            
if ($result){ 
   include 'showresults.php'; 
} else { 
    include 'poll.html'; 
}
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

the code you have shouldn't work...from the php manual:
Assuming the query succeeds, you can call mysql_num_rows() to find out how many rows were returned for a SELECT statment
and:
mysql_query() returns TRUE on success and FALSE on error.
what's happening is that mysql is sometimes throwing an error for some reason. stick with the mysql_num_rows thing...from the manual again:

Code: Select all

<?php

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

?>
and, finally, from a sample validation script I wrote:

Code: Select all

$result = mysql_query($query, $connection) or die('error making query');
	$affected_rows = mysql_num_rows($result);

	//if there's exactly one result, the user is validated. Otherwise, he's invalid
	if($affected_rows == 1) &#123;
		print "validated";
		//add the user to our session variables
		$_SESSION&#1111;'username'] = $user_name;
	&#125;
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Code: Select all

$result = mysql_query("SELECT * FROM poll WHERE ip=$ip"); 
            
if ($result)&#123; 
   include 'showresults.php'; 
&#125; else &#123; 
    include 'poll.html'; 
&#125;
All this will do is tell you whether or not the SQL was executed correctly, this will always be the case unless it is incorrectly coded, regardless of whether any records are returned.

Stick with the

Code: Select all

$num_rows = mysql_num_rows($result);
option.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

To find out exactly what mysql thinks is wrong use mysql_error() after the query:

Code: Select all

$sql = "SELECT field1, field2 FROM table";
$result = mysql_query($sql) or die(mysql_error().'<br />'.$sql;
llimllib is right, if mysql_num_rows() throws that error then there is an error with you connection or query.

The ONLY reason your code works is because this line:

Code: Select all

if ($result = 1)&#123;
sets $result to equal 1 no matter what was there before instead of comparing $result to the value one like this code would do:

Code: Select all

if ($result == 1) &#123;
One thing that might help is if you change your SQL statement from this:

Code: Select all

$sql = "SELECT * FROM poll WHERE ip=$ip";
to this

Code: Select all

$sql = "SELECT * FROM poll WHERE ip='$ip'";
Mac
Post Reply