Mikrotik Routerboard RB750UP simple email sign in hotspot

I have a couple of customers who want to give free WiFi to their guests but would also like the guest to sign up with their email address.

The Hotspot
Mikrotik do have an easy hotspot setup utility with their winbox software, so i going to assume that you are using that.

After you have setup the hotspot (plenty of documentaion out their for that)
You will need to start adjusting some files
Start with the main login.html in the Mikrotik
this should simply look like this (before i overwrite anything i made a backup of the original)
<html>
<head><title>...</title></head>
<body>
$(if chap-id)
<noscript>
<center><b>JavaScript required. Enable JavaScript to continue.</b></center>
</noscript>
$(endif)
<center>If you are not redirected in a few seconds, click 'continue' below<br>
<form name="redirect" action="http://www.yourdomainpagephp.php" method="post">
<input type="hidden" name="mac" value="$(mac)">
<input type="hidden" name="ip" value="$(ip)">
<input type="hidden" name="username" value="$(username)">
<input type="hidden" name="link-login" value="$(link-login)">
<input type="hidden" name="link-orig" value="$(link-orig)">
<input type="hidden" name="error" value="$(error)">
<input type="hidden" name="chap-id" value="$(chap-id)">
<input type="hidden" name="chap-challenge" value="$(chap-challenge)">
<input type="hidden" name="link-login-only" value="$(link-login-only)">
<input type="hidden" name="link-orig-esc" value="$(link-orig-esc)">
<input type="hidden" name="mac-esc" value="$(mac-esc)">
<input type="submit" value="continue">
</form>
<script language="JavaScript">
<!--
   document.redirect.submit();
//-->
</script></center>
</body>
</html>
This looks for an external page on your selected domain and you now beed to host a phppage
Now i used a wordpress page because i wanted to use a template which is friendly towards smart phones
so i installed a template called neuropro which has that feature.
Also on the wordpress template i needed a plugin called

PHP Code for posts Which in effect allows you to run php code inside a wordpress page. So here is the code which i used in html on a wordpress page <!--code begins here-->  [php snippet=1]
 
WiFi Hotspot
Please Fill in you details to use our Free WiFi service
<form method="POST" action="signin.php" name="form1">
First Name

<input type="text" name="firstname" size="30" />

Surname

<input type="text" name="lastname" size="30" />

Email address <span style="color: red;"> (required)</span>

<input type="email" name="email" size="30" />
<input type="submit" value="submit" />
</form> 
You need a php script to insert the email into a database so here is my signin.php   <?php


// Receiving variables
@$pfw_ip= $_SERVER['REMOTE_ADDR'];
@$firstname = addslashes($_POST['firstname']);
@$lastname = addslashes($_POST['lastname']);
@$email = addslashes($_POST['email']);
@$username = addslashes($_POST['username']);
@$date = addslashes($_POST['date']);


// Validation
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}

if (strlen($email) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}

//Sending auto respond Email to visitor
//$pfw_header = "From: info@cotswoldpc.co.uk\n"
//  . "Reply-To: info@cotswoldpc.co.uk\n";
//$pfw_subject = "This is your Username and Password for our Wifi";
//$pfw_email_to = "$email";
//$pfw_message = "Dear $firstname $lastname\n"
//. "Your Wifi username is $email\n"
//. "your Wifi password is $password\n"
//. "\n"
//. "Please enjoy our free wifi";
//@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;

//saving record to MySQL database
                       

                /* adding user information to the userinfo table */
                            @$pfw_strQuery = "INSERT INTO userinfo (username, firstname, lastname, email, creationdate) values ".
                    "('swaninn','$firstname', '$lastname', '$email', NOW())";
                         

@$pfw_host = "localhost";
@$pfw_user = "databaseuser";
@$pfw_pw = "databasepassword";
@$pfw_db = "databasename";

$pfw_link = mysql_connect($pfw_host, $pfw_user, $pfw_pw);
if (!$pfw_link) {
 die('Could not connect: ' . mysql_error());
}
$pfw_db_selected = mysql_select_db($pfw_db, $pfw_link);
if (!$pfw_db_selected) {
die ('Can not use $pfw_db : ' . mysql_error());
}

//insert new record
$pfw_result = mysql_query($pfw_strQuery);
if (!$pfw_result) {
 die('Invalid query: ' . mysql_error());
}
mysql_close($pfw_link);

header('Location: http://192.168.182.1/login?username=user2&password=user2');
die();

?>
The extra username is going into a table which is for reference only the part that makes this work is the Header location at the bottom in there you can see the username and password  are being passed back automatically so there is only 1 username and password for all users, just make sure you have multiple logins per user turned on in the hotspot. The ip is your local ip of the router, you also need to use HTTP PAP you then need to edit a further file alogin.html in the mikrotik hotspot alogin.html
 <html>
 <center>
 <head>
 <meta http-equiv="Refresh" content="0;url=http://www.redirectdomain.com" />
 </head>

 <body>
 </body>
 </center>
 </html>
This could be a splash page asking the guest to like your page on facebook Heres my config file for the microtik with firewall opened for port 80 and winbox and an ip address of 192.168.0.200 on the wan (port1) config file download So this hotspot simply asks for first name and surname and then an email address to login with no username or password required and stores to a database handy for the owner for newsletters etc. Have done a quick video showing the hotspot

TO note you should also add your external splash page website into the walled garden

56 comments:

  1. what happens what a duplicate email is entered?

    ReplyDelete
  2. Emails will duplicate in the database, when you export to csv and then import to a newsletter system like mailchimp it would only import the email address once. I use Phpmyadmin to delete duplicate emails but a script could be made to simply remove duplicate emails from the database.
    something like thuis could be use in phpmyadmin mysql query

    SELECT id FROM tablename NATURAL JOIN (
    SELECT email FROM tablename GROUP BY email HAVING COUNT(*) > 1) t

    thats if the field is called email

    ReplyDelete
  3. Figured out a slightly more elegant solution, cross references mac address with the database and if the user hasn't been seen before it will ask them to login otherwise they just get their entry updated.

    $query = "SELECT * FROM hotspot_info WHERE mac = '$mac'";
    $result = mysql_query($query) or die(mysql_error());

    if(mysql_num_rows($result)==0) {
    //NEW USER LOGIN
    } else {

    window.location = "http://10.4.1.1/login?username=user1&password=user1";


    <?php
    echo "spotted your mac address and stuff. your being re-logged.";
    mysql_query("UPDATE `hotspot_info` SET lastlogin=NOW(), status = 'active', count = count + 1, WHERE mac ='$mac'");
    exit;
    die();
    }

    Let me know what you think.

    ReplyDelete
    Replies
    1. Hi Man,

      Im trying to remove duplicate entries in the db, where would one implement your code above.

      thank you kindly in advance.

      Regards
      Wesley

      Delete
    2. This comment has been removed by the author.

      Delete
    3. DELETE FROM tablename WHERE NOT EXISTS (
      SELECT * FROM (
      SELECT MIN(id) minID FROM tablename
      GROUP BY email HAVING COUNT(*) > 0
      ) AS q
      WHERE minID=id
      )

      enter into phpmyadmin

      Delete
    4. Hi Tudor Gibson,

      Thank you for your reply, is this the only way as I wanted something automatic. or is it possible to make a script is virtual to run this command.

      I'm also trying to implement a capture to make sure this is not a bot logging in.

      Regards,
      Wesley

      Delete
    5. 2 things here, yes to run that as automatic, make a php script on your webserver, and you could use a cron say to run once a day.

      2nd rather than setup a capture, noindex the page or website that you intend to run on, that way no search engines find the page and you will not get any bother.

      Delete
    6. Hi Man i get the following using the following code of yours:

      DELETE FROM userinfo WHERE NOT EXISTS (
      SELECT * FROM (
      SELECT MIN(id) minID FROM userinfo
      GROUP BY email HAVING COUNT(*) > 0
      ) AS q
      WHERE minID=id
      )

      output:

      MySQL said: Documentation

      #1054 - Unknown column 'id' in 'field list'

      any help is appreciated.
      Regards,
      Wesley

      Delete
    7. Hi Tudor,

      With response to you last reply, the reason for the capture i guess is because i can enter an email address 1@2.3 and it will enter that info into the mysql db.

      as for the PHP script is this a simple xyz.php file on the webserver then accessed via the following in a cron job;

      cron command: wget ­q http://portal.prolocal.co.za/xyz.php 2>&1

      Regards,
      Wesley

      Delete
    8. do you have a column or field called id in your database?
      structure should be look this in first line on phpmyadmin of your table
      1 id int(11) UNSIGNED No None AUTO_INCREMENT

      yes you get the occasional person who enters say a@a.com but on the whole most people enter there emails legit, not many people are techies,

      yes to php script that would work fine depending on your server environment

      Delete
    9. Name Type Collation Attributes Null Default Extra Action
      id int(11) UNSIGNED No None AUTO_INCREMENT

      Delete
    10. Hi Tudor,

      Hope you are well, just a thanks for the previous help you gave me. all is working now.

      I just wanted to ask the following:

      Is it possible to have the hotspot automatic allow the user to get internet if they have logged in once before but add to the data base a column eg; "visited" and that would just show a number counter that states that this user has been here 5 times. but they only need to login on the first visit.

      maybe this can be done via mac address on the mikrotik?

      Regards,
      Wesley

      Delete
    11. and possibly update the the creation date column?

      Delete
    12. Im also interested in adding a link in the email sent to the user that says un-subscribe when the user clicks the link it would remove their email from the DB. is this possible too?

      any info or help is appreciated :-)

      Delete
    13. Great to here you got it going, the big thing that i got asked was in a pub for example users hated having to relogin every day, so in mikrotik i turned on cookie session and made it last for one week. in my friends cafe he wanted people to be logged in for no longer than an hour. so mikrotik can control this via cookie sessions. The problem with mac address is only one mac address is passed through to the webserver, that of the mikrotik wan. This is where you use a radius server to control logins a radius will remember macs and usernames. All i ever wanted was a quick solution to trapping email addresses to use for newsletters, i did not want to have to use a company for my solution who in the end would have to charge you money for their service.
      As far as updating creation date column this would have to be done with php scripting, so rule would be if email is duplicate update creation date. This however is far beyond my scope as i only do basic php.
      Not to say it cant be done with lots of googling.......

      Delete
    14. Hi Tudor,

      Thanks for your reply, I will look in to the cookies now on the mikrotik.

      and do you ave any ideas if some one wanted to un-subscribe from the newsletter they could click a link to remove their email from the db?

      thanks for your info man. this is so much fun.

      Regards,
      Wesley

      Delete
    15. If you google mikrotik hotspot sessions there is a lot on it, an unsubscribe would simply be a form almost reversing the action of what the input script is, something like enter your email to unsubscribe, then in that form would be the actions to search for that email address and remove. sort of like this article http://stackoverflow.com/questions/11856685/unsubscribe-html-form-using-php-and-my-sql

      Delete
    16. create a link on homepage that says unsubscribe that then links to a new page where they can enter there email address and it will remove from database

      Delete
    17. thanks man, will look into this now and revert back my findings.

      many thanks Tudor.....

      Delete
    18. Hi Patrick Clover, its a long time since you posted. I want to use your Mac checking refinement. But there are some errors. How to pass user mac to the database? Its not working. Can you help me. Please reply and I will tell you the problem in more detail. Thanks

      Delete
  4. That could be useful, was thinking of this, would have to put a mac field in database and would have to passthrough the mac address from mikrotik to webserver. I also implemented a simple php command at the end which disable usage of hotspot in the evening so the cafe owners dont have to turn of equipment

    mysql_close($pfw_link);

    if( date('G') >=8 && date('G')<=20 )
    {
    header('Location: http://192.168.182.1/login?username=user2&password=user2');
    die();
    }
    else
    {
    header('Location: http://www.cotswoldpchotspots.co.uk/?page_id=317');
    die();
    }

    this simply says if before 8pm let email be captured and proceed
    if after 8pm send to another page saying we are open in the morning bla bla

    ReplyDelete
  5. how do you manage your network remotely?

    ReplyDelete
  6. The mikrotik is on a static ip, and there is only 1 unifi box, but i have had no need to login as it always just works, the cafe owner in Oxford has had over 800 email signups in 6 months and is very pleased, the other pub i have it in has averaged 100 signups a month

    ReplyDelete
  7. is there a tutprial from scratch? im new to wordpress and io cant follow the tutorial. how can i reach you sir? do you have skype? please help me please?

    ReplyDelete
    Replies
    1. I would love to help but i am so busy at the mo
      a simple place to start would be to get a cheap hosting package with hostgator who have wordpress install scripts which makes wordpress install very easy, once you have wordpress installed why not reply let me know what you need next

      Delete
  8. was thinking i can use free webhosting, so i need to pay for web hosting, now thats my problem i have no money to pay. well thank you for your reply.. hope to find a alternative

    ReplyDelete
  9. How can I do the same thing and only give 30minutes access by using Mikrotik's only trial feature. But I still want to collect their email etc with a form.

    ReplyDelete
  10. hi can you upload again the config file ??
    the link is error
    thx
    br

    ReplyDelete
  11. Hi, Dropbox link not working, great writ up though

    ReplyDelete
  12. Is there any way to modify a hotspot user's password with a page residing on the hotspot router that I could let an administrator use, possibly with javascript?

    ReplyDelete
  13. How to contact u? I interesting your solution.
    My e-mail :mikrotikteamthai@gmail.com

    ReplyDelete
  14. Great article, but I do not understand how you've set the hotspot to accept authentication, you may send me a compat export or backup?

    ReplyDelete
    Replies
    1. The php file on the webserver sends this back to the mikrotik hotspot
      header('Location: http://192.168.182.1/login?username=user2&password=user2');
      die();
      this meeans that a username and password is sent back from the webserver php file in the url of the user giving them access
      just having a backup copy wont be enough you need to sdet up the form correctly on the webserver

      Delete
  15. hello,please can you send me the configuration file?
    please
    thanks

    ReplyDelete
  16. hey!! good job!! can you send to me your mikrotik config file or the script plz?

    ReplyDelete
  17. Bro please upload config file .

    regards

    ReplyDelete
    Replies
    1. sorry dropbox keeps changing link, updated now

      Delete
  18. Hi there, what is the username/password after I have restored the backup?
    Thank you

    ReplyDelete
  19. Hi Man,

    Hope you are well, great tutorial.

    if possible how can I chat to you, I have a few questions regarding the redirect from the mikrotik that I'm having some trouble with.

    Regards,
    Wesley

    ReplyDelete
  20. Hi Patrick

    Could you let me know where you inserted the MAC look up code

    Many Thanks

    Ian

    ReplyDelete
  21. i just wanna say thanks, it really help me out

    ReplyDelete
  22. Can someone implement this solution? I am more than happy to pay for it. Contact me at hsim7@hotmail.com

    ReplyDelete
  23. Can someone implement this solution? I am more than happy to pay for it. Contact me at hsim7@hotmail.com

    ReplyDelete
  24. Anyone interested with the solution, we can help you to implement it, contact us at info@balitechdude.com

    ReplyDelete
  25. This comment has been removed by a blog administrator.

    ReplyDelete
  26. Hello
    What is the login and pwd for restored hotspot.backup?
    Thanks

    ReplyDelete
  27. hello, very interesting post. I downloaded to have a look, but i cannot enter anymore in mikrotik, can you please give the username/pass.

    ReplyDelete
  28. hello,

    can you send me html code with all the php in it?

    Regards

    ReplyDelete
  29. Could you do it for login with facebook, instead login with email account?

    ReplyDelete
  30. I mean, to have free wifi access, just login into facebook account or just push Like button ???? Thanks in advance.

    ReplyDelete
  31. Please try our social hotspot login at http://www.cloud-hotspot.com

    ReplyDelete
  32. Can anyone help me to create facebook page like button login hotspot in mikrotik.
    sarfaraz.leghari@hotmail.com

    ReplyDelete
  33. After restore, my winbox cannot login. What user n pass?

    ReplyDelete