I need to make an anti-multi account, i found this example for the mysql query http://sqlfiddle.com/#!9/49bea0/1 that, is that a good way to do it?
If there are other simple ways to do, please let me know this is really important 🙁
Thank you.
Advertisement
Answer
It sounds to me like you want to reject attempts to sign up for accounts if they come from an IP address that already has an account.
The comments have raised the question of whether this is a good idea. It is not. For one thing it’s easy for a user to change IP addresses. For another thing, some companies and universities have thousands of users sharing a single public-network IP address (look up NAT router to learn more). Even two different home users in the same household share a single IP, so I can have an account and my partner can’t. So allowing only one account per IP address is an unnecessary and insufficient security measure.
But that’s not what you asked. You asked how to do it with SQL. Here’s how.
Let’s say you have a table called users
with a column called ip_address
.
Create a unique index on that column.
CREATE UNIQUE INDEX no_dup_ip ON users (ip_address);
Then, when you try to use an INSERT statement to add a new user — a new row to your users
table — it will fail if it has the same IP address as any existing user.
You will get MySQL error 1062, with a text error message looking something like this.
Error Code: 1062. Duplicate entry ‘192.0.2.210’ for key ‘no_dup_ip’.
You can detect this error with the PDO or mysqli error reporting functions, and get your php program to say something polite to your would-be user.