dashcrash Posted January 2, 2011 Share Posted January 2, 2011 Hello all, I am a total noob in php and some VIP asked me to make something simple for our clubhouse; A page where users can submit/input 2 codes through a textbox and store it in a database. There are 2 things i want: max 2 submits/inputs per IP(to prevent abuse)(ip stored in database). $ip = $_SERVER['REMOTE_ADDR']; $check = mysql_query("select * from table where ipcolumn='$ip'"); if (mysql_num_rows($check) > 0) $insertip = "insert into table (ipcolumn) values ('$ip')"; And i wanna catch the error when an user put in a code lower than 5 characters. not something like: if ($textarea == ""){ This is where i am so far. new.php <?php include 'includes/config.php'; mysql_select_db($mysql_database, $con); $sql="INSERT INTO users (code, code2) VALUES ('$_POST[code]','$_POST[code2]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }else{ header ("Location: /thanks.php"); } ?> includes/config.php <?php $mysql_host = "myhost.com"; $mysql_database = "mydb"; $mysql_user = "myuser"; $mysql_password = "mypass"; $con = mysql_connect($mysql_host,$mysql_user,$mysql_password); if (!$con) { die('Could not connect: ' . mysql_error()); } ?> sql.sql -- -- Table structure for table `users` -- CREATE TABLE `users` ( `code` varchar(30) collate latin1_general_ci NOT NULL, `code2` varchar(32) collate latin1_general_ci default NULL, `timestamp` int(11) unsigned NOT NULL, PRIMARY KEY (`code`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; index.html <form action="new.php" method="post" onsubmit="return onSubmitButton();"> <input value="" id="something" class="LoginForm-Input" name="code1" size="34" style="float: left;"> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/223181-limit-2-accountsusers-per-ip/ Share on other sites More sharing options...
trq Posted January 2, 2011 Share Posted January 2, 2011 So you are stuck where? Quote Link to comment https://forums.phpfreaks.com/topic/223181-limit-2-accountsusers-per-ip/#findComment-1153781 Share on other sites More sharing options...
dashcrash Posted January 2, 2011 Author Share Posted January 2, 2011 Lol? Quote Link to comment https://forums.phpfreaks.com/topic/223181-limit-2-accountsusers-per-ip/#findComment-1153782 Share on other sites More sharing options...
dashcrash Posted January 2, 2011 Author Share Posted January 2, 2011 edit: i cannot add the 2(lets say) modules(max user per ip and min. 5 charactars per textbox), it either gives me an error or somethign else happens. So i seperated the workign and not wkring code and posted it above so maybe some1 else can fix it. 2ns edit:I m sorry but English isnt my first language, so if i need to explain something, just tell me. Quote Link to comment https://forums.phpfreaks.com/topic/223181-limit-2-accountsusers-per-ip/#findComment-1153784 Share on other sites More sharing options...
dashcrash Posted January 2, 2011 Author Share Posted January 2, 2011 Ok i tried some stuff but i cant get it fixed Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /public_html/new.php on line 8 Error: Duplicate entry '' for key 1 <?php include 'includes/config.php'; //check for ip $ip = $_SERVER['REMOTE_ADDR']; $check = mysql_query("select * from users where IP='$ip'"); if (mysql_num_rows($check) > 0) { echo "Sorry you have already submitted a code from this IP"; } else { //carry on processing $insertip = "insert into table (IP) values ('$ip')"; mysql_query($insertip); //carry on processing } // Code to short Error Message $ErrorMsg = "Code needs to be longer than 5"; // Set minium code length $minpass = 5; // If code is less than $minpass, created error message if($_POST['code'] < $minpass) { $ErrorMsg .= "Code needs to be longer than 5"; } mysql_select_db($mysql_database, $con); $sql="INSERT INTO users (code, code2) VALUES ('$_POST[code]','$_POST[code2]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }else{ header ("Location: /thanks.php"); } ?> New sql: -- -- Table structure for table `users` -- DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `code` varchar(30) collate latin1_general_ci NOT NULL, `code2` varchar(32) collate latin1_general_ci default NULL, `IP` varchar(25) collate latin1_general_ci NOT NULL default '', `timestamp` int(11) unsigned NOT NULL, PRIMARY KEY (`code`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; [/code] Quote Link to comment https://forums.phpfreaks.com/topic/223181-limit-2-accountsusers-per-ip/#findComment-1153793 Share on other sites More sharing options...
fortnox007 Posted January 2, 2011 Share Posted January 2, 2011 not sure if this is helpful, but is your primary key set on auto increment? Quote Link to comment https://forums.phpfreaks.com/topic/223181-limit-2-accountsusers-per-ip/#findComment-1153807 Share on other sites More sharing options...
litebearer Posted January 2, 2011 Share Posted January 2, 2011 an aside: limiting user(s) by IP is not the best method - example: I have home computer IP1 make 2 entries; Office computer IP2 make 2 enteries; Laptop wifi at coffee shop IP3 make 2 entries - result 1 user has made 6 entries Quote Link to comment https://forums.phpfreaks.com/topic/223181-limit-2-accountsusers-per-ip/#findComment-1153832 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.