View Full Version : Submitting a form according to IP

01-02-2008, 06:29 PM
Is there a way to have a form check the mysql DB IP field if the IP matches that of the person filling out te form it displays message or grays out the submitt button

01-02-2008, 07:45 PM
This hasn't been tested but should work:

// Database Login Function
function db_connect()
$result = mysql_pconnect("localhost", "USERNAME", "PASSWORD");
if (!$result)
return false;
if (!mysql_select_db("DATABASE_NAME"))
return false;

return $result;

// Get Computers IP address
$ip_address = $_SERVER['REMOTE_ADDR'];

// Search From IP Address Database
$sql="SELECT * FROM banned_ips WHERE ip_address = '".$ip_address."'";
$query = mysql_query($sql, db_connect()) or die(mysql_error());
$num_rows = mysql_num_rows($query);

// IP Address was not found
if ($result["ip_address"] == ""){

//IP address found display the submit button.

echo '<input type="submit" name="button" id="button" value="Submit">';

} else {

// IP address is banned display message
echo 'Sorry you are not allowed to use this form!';



01-02-2008, 08:45 PM
couldnt get it to work

01-03-2008, 03:13 AM
The idea is something like this

$ip_address = $_SERVER['REMOTE_ADDR'];

The above code will give you the client IP address. You can do whatever you want with it. I mean at the time of store it in your db and later check it with incoming requests. But what I feel is it may not provide you with the needed situation always.

01-03-2008, 03:30 AM
I was trying to figure out a way to prevent the same person from being able to submit more than once. It’s not real critical, but a deterrence. Only a few will be post to the form and their not real savvy about there IP so for what I’m trying to accomplish it would be good.

I tried the above code several differant ways and it displayed
echo '<input type="submit" name="button" id="button" value="Submit">';} else {// IP address is banned display messageecho 'Sorry you are not allowed to use this form!';}
At the top of the form page with everything I tries.
My submit button is <input type='submit' value='Submit Form'> which is going here <form enctype='multipart/form-data' action='form.php' method='post' name="Enter_Form"

01-07-2008, 03:59 PM
post you whole page php code for me to review

01-07-2008, 06:45 PM
okay well upon entering the site you should check their IP.

as code_exploiter said
$ip_address = $_SERVER['REMOTE_ADDR']; will give you the IP Address of the browser.
From there, you can processes it as necessary. For your concerns you want two checks. First you want to see if its on some "banned ip" list, and some "already posted" list. Both sets should be stored somewhere on the server, either in a text field or a database table. It would probably be easiest for the "Already Posted" to be in the posts table, however the banned IP has no defined place that they are. If you put in a text field you can check the IP, however if you have a database table you can see other meta information alot easier, like date/time banned, who banned, why was person banned? This can all be done in a text file too, but some people find it easier to do in a table ...

So you just need to create a query that will take the IP Address of the user as explained above and run it again both of those lists... if it's a hit then you display some error message, if not it means that the user can make a post.