Registering Via IP (Continuation) in PHP/MySQLi

Language
Introduction: This tutorial is on how to allow our users register via their unrecognised IP (Continued from http://www.sourcecodester.com/php/7539/auto-recognise-users-ip-address-through-phpmysqli.html). Important: 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! Validation: 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.         <heaD>
  2.         </head>
  3.         <body>
  4.  
  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.  
  3. }
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')");
Finished! 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>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment