473,756 Members | 8,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP & MYSQL ip address check

5 New Member
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 6677
Atli
5,058 Recognized Expert Expert
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($QU ERY) or die(mysql_error ());

// Check how many rows the query returned
if(mysql_num_ro ws($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($QU ERY) or die(mysql_error ());

// Read the firs row
$row = mysql_fetch_ass oc($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 New Member
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
2870
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,   `allegato` varchar(255) default NULL,   `descrizione` varchar(255) default NULL,   `online` enum('true','false') NOT NULL default 'true',   PRIMARY KEY  (`id`),
4
2812
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 http://192.168.1.106/~mlh/credifree/index.php the AMP app still thinks the data resides somewhere else. It runs fine - as long as I leave my LAN's external internet connection up. But if I unplug my LAN from the world, my app locks up. Before I...
16
27923
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 own LAN, the driver tells me it cannot make the connection. Here are the ODBC driver connection parms: Data Source Name: (free field - name my "my linux box" will do nicely) Host/Server Name (or IP) - something like MSQLUserName@ServerName.net...
8
2164
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 and the "pear" Db .php. I recently upgraded my Apache to 2.2 and installed (more or less) PHP 5.2 as well - neither of these installs were from a Redhat rpm, I downloaded the tarballs, extracted, configed, make, etc. Apache seems to work fine...
0
5573
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
18
2247
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
4366
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 these new sets of codes have overwhelmed me a bit. Here's the new code: CREATE TABLE `users` ( `ID` int(11) NOT NULL auto_increment, `Username` varchar(255) NOT NULL, `Password` varchar(255) NOT NULL, `Temp_pass` varchar(55)...
6
1262
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 do-it-yourselfer as far as web design goes and definately won't be coding my own php or mysql in this lifetime. i generate alot of questions too. can pay on a per instance basis for the project or for answers to questions if you are cool with being a...
5
2940
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 works fine in FF and IE. It's Chrome I'm having issues with. I understand that Chrome is still somewhat in beta stages, so some bugs might occur. However this seems like something I might have done. So... I used a code that I found on Econym as...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.