473,324 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

PHP & MYSQL ip address check

5
Expand|Select|Wrap|Line Numbers
  1. <html> 
  2. <head> 
  3. <title>Add New MySQL User</title> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
  5. </head> 
  6. <body> 
  7. <? 
  8. if(isset($_POST['add'])) 
  9.  
  10. include 'c:\sokkit\config.php'; 
  11. include 'c:\sokkit\opendb.php'; 
  12. $ip=$_SERVER['REMOTE_ADDR'];
  13. $username = $_POST['username']; 
  14. $password = $_POST['password'];
  15. $country = $_POST['mydropdown'];
  16.  
  17. $query = "INSERT INTO account (username, password, location, forumip) VALUES ('$username', '$password', '$country', '$ip')"; 
  18. mysql_query($query) or die('Error, insert query failed'); 
  19.  
  20. $query = "FLUSH PRIVILEGES"; 
  21. mysql_query($query) or die('Error, insert query failed'); 
  22.  
  23. include 'c:\sokkit\closedb.php'; 
  24. echo "New MySQL user added"; 
  25. else 
  26. ?> 
  27. <form method="post"> 
  28. <div align="left">
  29. <select name="mydropdown">
  30. <option value="Zimbabwe">Zimbabwe</option>
  31. </select>
  32. </div>
  33. <table width="400" border="0" cellspacing="1" cellpadding="2"> 
  34. <tr>  
  35. <td width="100">Username</td> 
  36. <td><input name="username" type="text" id="username"></td> 
  37. </tr> 
  38. <tr>  
  39. <td width="100">Password</td> 
  40. <td><input name="password" type="text" id="password"></td> 
  41. </tr> 
  42. <tr>  
  43. <td width="100">&nbsp;</td> 
  44. <td>&nbsp;</td> 
  45. </tr> 
  46. <tr>  
  47. <td width="100">&nbsp;</td> 
  48. <td><input name="add" type="submit" id="add" value="Add New User"></td> 
  49. </tr> 
  50. </table> 
  51. </form> 
  52. <? 
  53. ?> 
  54. </body> 
  55. </html> 
  56.  
this is an account creation I am trying to put together. Totally newbie in php and html i am no better, so please bare with me. So far everything works good. When I submit, it writes everything including the ip in mysql database. As you can see $ip is the variable for ip address. What I am looking to do is prevent anyone from doing multiple creates from same ip. I have learned how to write to the mysql database, but not learned yet how to read. I will need to read from the "account" table and "forumip" field to see if the ip exists.
kinda like this:
select * from `account` where forumip=$ip';

So basically when someone presses submit, if ip in database already to error rather then submitting the info.
Any help in this area is greatly appreciated.
Apr 17 '07 #1
2 6661
Atli
5,058 Expert 4TB
Hi.

There is a very simple way of querying the database and counting the rows it returns. In your case, if it returns more than 0 rows you would not add the new user.
Like this:
[PHP]
// Create the query and execute it
$QUERY = "SELECT field FROM table WHERE field = value";
$RESULT = mysql_query($QUERY) or die(mysql_error());

// Check how many rows the query returned
if(mysql_num_rows($RESULT) > 0) {
echo "The value already exists";
}
else {
// Put your insert code here.
}
[/PHP]

This is very simple and will do fine in your case, because your design only allows for one row to be return.

However, if you were to use this method to count, for example, every single row in your table, this would return a lot of data you dont need.
In that kind of a situation I would rather reccomend an approach like this:
[PHP]
// Create query and execute
$QUERY = "SELECT COUNT(field) AS 'count' FROM table WHERE field = value";
$RESULT = mysql_query($QUERY) or die(mysql_error());

// Read the firs row
$row = mysql_fetch_assoc($RESULT);

// Check how many rows MySQL counted
if($row['count'] > 0) {
echo "value already exists";
}
else {
// Put input code here
}
[/PHP]

This way MySQL only returns one row no matter how many it counts, which will lighten the load on the server on a busy day, especially if the MySQL server is on another server.

Hope this helps.
Apr 17 '07 #2
Boujii
5
Thanks Atli. That titbit of code worked great. :)
Apr 17 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: sathia | last post by:
Hi, I have this table:   CREATE TABLE `osservatorio` (   `id` int(10) unsigned NOT NULL auto_increment,   `testo` varchar(255) NOT NULL default '',   `parent` int(11) default NULL,...
4
by: MLH | last post by:
A programmer developed an AMP (Apache/MySQL/PHP) application for me. When he was done, he sent me the PHP files and the MySQL dump file. Now, when I connect to the application on my LAN using...
16
by: MLH | last post by:
Using MS Access, I have attached to MySQL servers in other states and other countries on the other side of my router. But when I use the MySQL ODBC driver 3.51 to connect to a MySQL server on my...
8
by: eholz1 | last post by:
Hello Newsgroup, I have redhat 3.0 Enterprise, and mysql (3.58.xxx more or less) installed from a redhat rpm, and php 4.3.9 (installed from an rpm) - I can access data from my mysql db using php...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
18
by: Bruce A. Julseth | last post by:
I have the following code $Host = "localhost"; $User = "Fred"; $Database = "house"; $Password = "mypw" echo "before mysqli<br />Host: " . $Host . "<br />" . $User . "<br />" . $Database;
1
George Lft
by: George Lft | last post by:
ok, first of all, i built my register page using dreamweaver tool which the codes haven been out of control. Now i'm thinking that turning over everything - by using this another set of codes. And...
6
by: mike | last post by:
hi. trying to set up a new site with some interactivity between the pages. need a coder to set up the initial user database where visitors create their personal accounts. i'm a green...
5
by: Nike1984 | last post by:
I'm fairly new to Javascript and it's more of a guessing game for me... I'm trying to build an app for Google Maps and just had some issues recently. First off I just wanted to say that everything...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.