LOGO
Develop With Me is a Website Will Lots Of Free Source Code.
You Can Download And Modify Code And Also U Can Develop New Code With Our Team And Join Us.

Registering Via IP (Continuation) in PHP/MySQLi


Submitted by: 
Admin
Language: 
PHP





Introduction:
This tutorial is on how to allow our users register via their unrecognised IP .
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. <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')");
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>

Previous
Next Post »