On the Ubiquiti forums I've seen a lot of requests to create a form to capture the Guest Users information from an External Guest Portal. I wanted to do the same thing so I created a write-up to show how's how I did it with UniFi versin 2.4.6. This may not be the best way, but it worked for me :) Please note: this is not my final product it is just a sample to show how it can be done.
To get this to work, I started with the External PHP Guest Portal Script from jMichieli in the Ubiquiti Community and a MySQL database. To make sure I give credit where credit is due, the original post can be found here, Ubiquiti Community Guest Portal.
First, create the Database that will contain the guest user information that you want to capture. This article is not intended to teach you how to create a database and a table. If you don't know how to do this, you'll need to find someone that does.
I called my database UniFi and I created a Guest table that includes the following fields
• GuestID - This is a auto incrementing field that is the primary key for the database.
• FirstName - Captures the users First Name
• LastName - Captures the users Last Name
• emailAddress - Captures the email address
• Age - Allows the users to enter their age. This may not be needed but I put it in anyway. I don't think I'll keep this one for my final code.
• id - this is the MAC address of the device that requested guest access.
• datetime - This field is used to capture the date and time the form was submitted.
You could use the id as the primary field because the MAC address will always be unique. I did not go this route because I wanted to keep this simple and I also wanted to see the dates and times the user connected with the MAC address.
Once you create the database, you can use the script below in MyPHPAdmin to create the Guest table with the appropriate settings.
CREATE TABLE IF NOT EXISTS `Guest` (
`GuestID` int(11) NOT NULL AUTO_INCREMENT,
`FirstName` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`LastName` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`emailAddress` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`Age` int(3) NOT NULL,
`id` varchar(17) COLLATE utf8_unicode_ci NOT NULL,
`datetime` datetime NOT NULL,
PRIMARY KEY (`GuestID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Now that we have the database setup, simply grab the sample php code, modify it and upload it to your webserver in the /guest directory.
What do you need to modify?
- For testing purposes, the index.php should not need to be modified. However, you should change this for your production site by adding form validation or changing the size of the input boxes.
- The SQL Connection to be updated
- UniFi Controller information needs to be updated
- Validate Results are correct
On line 4 of authorized.php you will need to change the sample database connection string with the information to connect to your database.
You will need to enter your hostname, db user name, db password, and the db name in the proper locations on line 4.
On lines 36 - 38 you need to enter in your UniFi controller information for this to work fully. If you are just testing the solution to see if it logs the information to your MySQL server, you don't have to enter in your controller information but you will need to know the format of the URL string you have to pass for the test to work correctly. I'll have more information on that below.
On lines 90 - 97 I've added code so you can verify that the variables are working correctly and that you are getting the correct result.
FirstName: <?php echo $_POST['firstname']; ?><br />
LastName: <?php echo $_POST['lastname']; ?><br />
email address is: <?php echo $_POST['emailaddress']; ?><br />
Age <?php echo $_POST['age']; ?><br />
Mac Address <?php echo $_POST['id']; ?><br />
Date: <?php echo $datetime; ?><br /><br />
Testing that it works
To test that it works, is fairly simple. Create your own test URL string by constructing a URL that contains your domain name or ip address followed by the mac address of your PC, the mac address of you AP, a timestamp, and then the SSID of your guest network. For example: http://mydomain.com/guest?id=a8:06:22:c3:a9:27&ap=24:a4:3c:34:ab:23&t=1397571605&ssid=GUEST_WIFI#
If you are connecting to your UniFi controller, I suggest using a mac address from one of your own access points and one of your guest SSIDs to ensure that you don't encounter any errors.
If configured properly, you should be prompted to enter your information
Once you hit connect, your Results Validation should show in your browser
When you look at your MySQL database you should see the users information captured.
In the future I've thought about capturing:
• The MAC address of the AP that the user connected to initially
• Users Phone Number
• Comments field on why the user connected.