Jump to content

How to check if result exists in mysql table


jdock1

Recommended Posts

Basically what im doing is trying to check if a new users ip exists in the database, if so theup.n it will prevent the account signup.

 

I already have a table with the ip lists, but I need to match them, to see if it already exists.

 

Surely theres a way to do this? I already did my research, so please, don't  " :rtfm:" me!

 

Thanks guys!!!!

Link to comment
Share on other sites

$query = "SELECT count(*) as countof FROM iptablename WHERE ip = '{$_SERVER['REMOTE_ADDR']}'";

 

Fetch the result. 

 

Really bad idea unless you don't care about stopping people who share an IP (NAT'd) from signing up.

Link to comment
Share on other sites

$query = "SELECT count(*) as countof FROM iptablename WHERE ip = '{$_SERVER['REMOTE_ADDR']}'";

 

Fetch the result. 

 

Really bad idea unless you don't care about stopping people who share an IP (NAT'd) from signing up.

Hmm im not sure I really understand the code. Would it work like this?

$query = "SELECT count(*) as countof FROM mytable WHERE ip = '{$_SERVER['REMOTE_ADDR']}'";
mysql_query($link,$query);
if ($query) {
echo "Duplicate IP";
}

So my understanding is that query is looking for the same IP in the table already? If thats so, then my code is valid, if that query is true & it found a matching IP, it echos the error?

 

Also, I have been thinking about that. Do alot ISPs issue the same IP? How common is that?

& what do you mean by nat'd?

 

Sorry for all the questions! Thanks for your reply& advice!

Link to comment
Share on other sites

This may be the longer way around than using Count in the query, but doing something like this always works for me.

$result = mysql_query("SELECT * FROM iptablename WHERE ip = '{$_SERVER['REMOTE_ADDR']}'", $link);
$num_rows = mysql_num_rows($result);

if($num_rows>"0")
{
// do not let them sign up
}

else
{
// display signup code
}

 

Link to comment
Share on other sites

Your code needs to actually look at the result of the count(*) query.

 

$query = "SELECT count(*) as countof FROM mytable WHERE ip = '{$_SERVER['REMOTE_ADDR']}'";
$result = mysql_query($link,$query);
if ($result) {
  $row = mysql_fetch_assoc($result);
  if ($row['countof'] > 0) {
     // don't allow signup
  } else {
    // do signup

  }
}

Link to comment
Share on other sites

Also, I have been thinking about that. Do alot ISPs issue the same IP? How common is that?

& what do you mean by nat'd?

 

Well, colleges/universities, hotels, apartments, Tim Hortons, any place like this that has free internet or some form of networking (like in a college) is most likely going to have one IP. Or at least not a unique IP for every user. So, if one person from a college signs up on your website then nobody else in that college can.

 

Plus, this is a rather useless form of protection because anyone can just go behind a proxy and still sign up multiple accounts.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.