Google Ad:

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!

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

 

 

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

 

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…

 

 

    $shouldShow = 0;

 

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

 

 

    $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-

 

 

    <html>
          <heaD>
          </head>
          <body>
     
          </body>
    </html>

 

 

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

 

 

    <?php
          if ($shouldShow > 0) {
                //True, not registered yet
                echo "
                ";
          }//Else; Already Registeted. No form.
    ?>

 

 

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…

 

 

    <form action='ip.php' method='POST'>
          Username: <input type='text' name='username' />
          <br/>
          IP: ".$ipaddr."
          <br/>
          <input type='submit' value='Register!' name='submitted' />'
    </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.

 

 

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

 

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

 

 

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

 

Finished!
Full source:

 

 

    <?php
          $con = mysqli_connect('localhost', 'root', '', 'fln'); //server, username, password, database name
          if (isSet($_POST['username'])) {
                $username = $_POST['username'];
                $ipaddr = $_POST['ipaddr'];
                $q = mysqli_query($con, "INSERT INTO `test` VALUES ('', '$username', '$ipaddr')");
          }
          $shouldShow = 0;
          $ipaddr = $_SERVER['REMOTE_ADDR'];
          $q = mysqli_query($con, "SELECT * FROM `test` WHERE `ip`='$ipaddr'");
          if (mysqli_num_rows($q) > 0) {
                echo 'Is your username ' . mysqli_fetch_array($q)['username'] . '?';
          }else{
                echo 'No username found by that IP address!';
                $shouldShow = 1;
          }
    ?>
    <html>
          <heaD>
          </head>
          <body>
                <?php
                      if ($shouldShow > 0) {
                            //True, not registered yet
                            echo "
                                  <form action='ip.php' method='POST'>
                                        Username: <input type='text' name='username' />
                                        <br/>
                                        IP: ".$ipaddr."<input type='hidden' value='".$ipaddr."' name='ipaddr' />
                                        <br/>
                                        <input type='submit' value='Register!' name='submitted' />'
                                  </form>
                            ";
                      }//Else; Already Registered. No form.
                ?>
          </body>
    </html>

 

 

 

Read Me

 



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

Have any Question or Comment?

Leave a Reply

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

Categories