Jump to content

Limit 2 Accounts/Users per ip


dashcrash

Recommended Posts

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]

Link to comment
https://forums.phpfreaks.com/topic/223181-limit-2-accountsusers-per-ip/
Share on other sites

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.

Ok i tried some stuff but i cant get it fixed  :confused:

 

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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.