Page 1 of 1
ip blocking
Posted: Wed Jul 03, 2002 9:13 pm
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.
Posted: Wed Jul 03, 2002 9:54 pm
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';
Posted: Wed Jul 03, 2002 10:07 pm
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';
Posted: Wed Jul 03, 2002 10:25 pm
by llimllib
good call, sloppy programming on my part
Posted: Wed Jul 03, 2002 11:30 pm
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....
Posted: Wed Jul 03, 2002 11:38 pm
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
Posted: Wed Jul 03, 2002 11:53 pm
by hob_goblin
Code: Select all
$result = mysql_query("SELECT * FROM poll WHERE ip=$ip");
if ($result){
include 'showresults.php';
} else {
include 'poll.html';
}
Posted: Thu Jul 04, 2002 6:32 am
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) {
print "validated";
//add the user to our session variables
$_SESSIONї'username'] = $user_name;
}
Posted: Thu Jul 04, 2002 6:42 am
by Wayne
Code: Select all
$result = mysql_query("SELECT * FROM poll WHERE ip=$ip");
if ($result){
include 'showresults.php';
} else {
include 'poll.html';
}
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.
Posted: Thu Jul 04, 2002 6:49 am
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:
sets $result to equal 1 no matter what was there before instead of comparing $result to the value one like this code would do:
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