Google Ads

Registering Via IP (Continuation) in PHP/MySQLi PHP Script

Registering Via IP (Continuation) in PHP/MySQLi PHP Script

I highly recommend that you read through the linked tutorial above, it will allow you to get the same source I am going to be using throughout this tutorial!

So before we can allow them to register, we need to validate that they are not already registered and therefore IP recognised.


  1. $q = mysqli_query($con, "SELECT * FROM `test` WHERE `ip`='$ipaddr'");
  2. if (mysqli_num_rows($q) > 0) {
  3.       echo 'Is your username ' . mysqli_fetch_array($q)['username'] . '?';
  4. }else{
  5.       echo 'No username found by that IP address!';
  6. }


Now we can see that the else block of code above, is for if the user is not yet registered.

So if the else block of code gets executed, we want to first set an integer variable to used as a boolean (1=true, 0=false), call it ‘shouldShow’. Make this globally available…


  1. $shouldShow = 0;


Now in the else block, set it to ‘1’, true…


  1. $shouldShow = 1;



HTML Form:
To get the user information, we want a simple HTML form for their input username, we first check whether we should show the HTML form, by referring to our PHP variable earlier…

-Basic HTML Template-


  1. <html>
  2.       <heaD>
  3.       </head>
  4.       <body>
  5.       </body>
  6. </html>



Now, we need to use PHP to check the PHP variable, so don’t forget the PHP tags.


  1. <?php
  2.       if ($shouldShow > 0) {
  3.             //True, not registered yet
  4.             echo "
  5.             ";
  6.       }//Else; Already Registeted. No form.
  7. ?>



In the echo statement we put the HTML form. We simply take one text field of the username, and output their IP. Along with a submit button of course. The form automatically uses the secure POST (as opposed to GET) method, and action is the same page file name as we are currently editing…


  1. <form action='ip.php' method='POST'>
  2.       Username: <input type='text' name='username' />
  3.       <br/>
  4.       IP: ".$ipaddr."
  5.       <br/>
  6.       <input type='submit' value='Register!' name='submitted' />'
  7. </form>



Form Processing:
Finally we want to now check if the POST data is received/sent on the page load, so at the top of the page we check for the username POST.


  1. if (isSet($_POST['username'])) {
  2. }


And if it is there, we insert a new row in to our ‘test’ table with the information.


  1. $username = $_POST['username'];
  2. $ipaddr = $_POST['ipaddr'];
  3. $q = mysqli_query($con, "INSERT INTO `test` VALUES ('', '$username', '$ipaddr')");


Full source:


  1. <?php
  2.       $con = mysqli_connect('localhost', 'root', '', 'fln'); //server, username, password, database name
  3.       if (isSet($_POST['username'])) {
  4.             $username = $_POST['username'];
  5.             $ipaddr = $_POST['ipaddr'];
  6.             $q = mysqli_query($con, "INSERT INTO `test` VALUES ('', '$username', '$ipaddr')");
  7.       }
  8.       $shouldShow = 0;
  9.       $ipaddr = $_SERVER['REMOTE_ADDR'];
  10.       $q = mysqli_query($con, "SELECT * FROM `test` WHERE `ip`='$ipaddr'");
  11.       if (mysqli_num_rows($q) > 0) {
  12.             echo 'Is your username ' . mysqli_fetch_array($q)['username'] . '?';
  13.       }else{
  14.             echo 'No username found by that IP address!';
  15.             $shouldShow = 1;
  16.       }
  17. ?>
  18. <html>
  19.       <heaD>
  20.       </head>
  21.       <body>
  22.             <?php
  23.                   if ($shouldShow > 0) {
  24.                         //True, not registered yet
  25.                         echo "
  26.                               <form action='ip.php' method='POST'>
  27.                                     Username: <input type='text' name='username' />
  28.                                     <br/>
  29.                                     IP: ".$ipaddr."<input type='hidden' value='".$ipaddr."' name='ipaddr' />
  30.                                     <br/>
  31.                                     <input type='submit' value='Register!' name='submitted' />'
  32.                               </form>
  33.                         ";
  34.                   }//Else; Already Registered. No form.
  35.             ?>
  36.       </body>
  37. </html>




Related Post

FB Inboxer v5.1.3 – Master Facebook Messenge... FB Inboxer v5.1.3 - Master Facebook Messenger Marketing Software - nulled PHP Script Facebook Inboxer is a revolutionary, world’s very first,most p...
Add Partner New PHP Script Add Partner New PHP Script You can create an Add Partner in which you can add, delete etc.   Download File  
Create Login and Logout Page with Session and Cook... Create Login and Logout Page with Session and Cookies PHP Script n my previous tutorial, I've shown you How to Set up Cookie. This tutorial will gi...
E-Commerce/Shopping Cart Web Application PHP Scrip... E-Commerce/Shopping Cart Web Application PHP Script Amaclone is an e-commerce website built using PHP, Bootstrap and some jQuery plugins. It ...
Crypty 2.0 PHP Script Download Crypty 2.0Crypty 2.0In the last few years encryption is a topic which rapidly came to mind to almost all site owners on the web. Nowadays end users wo...

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *