Registering Via IP (Continuation) in PHP/MySQLi
Submitted by Yorkiebar on Wednesday, July 9, 2014 - 09:45.
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...
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...
Now in the else block, set it to '1', true...
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-
Now, we need to use PHP to check the PHP variable, so don't forget the PHP tags...
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 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...
And if it is there, we insert a new row in to our 'test' table with the information...
Finished!
Full source:
- }else{
- echo 'No username found by that IP address!';
- }
- $shouldShow = 0;
- $shouldShow = 1;
- <?php
- if ($shouldShow > 0) {
- //True, not registered yet
- echo "
- ";
- }//Else; Already Registeted. No form.
- ?>
- <form action='ip.php' method='POST'>
- Username: <input type='text' name='username' />
- <br/>
- IP: ".$ipaddr."
- <br/>
- <input type='submit' value='Register!' name='submitted' />'
- </form>
- }
- $username = $_POST['username'];
- $ipaddr = $_POST['ipaddr'];
- <?php
- $username = $_POST['username'];
- $ipaddr = $_POST['ipaddr'];
- }
- $shouldShow = 0;
- $ipaddr = $_SERVER['REMOTE_ADDR'];
- }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>
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
- 49 views