Syed Jahanzaib – Personal Blog to Share Knowledge !

February 10, 2014

Hotspot User Change Password FORM for ‘User Self Management’

Filed under: Mikrotik Related — Tags: , — Syed Jahanzaib / Pinochio~:) @ 11:18 AM

How to provide Mikrotik Hotspot Users an option to change there password using any form or web page?

The simple answer is to configure USER MANAGER and provide User Panel which is very nice and informative, it also allows users to change there password too, but what if you don’t want to install User Manager, or what if user also change his information via the user panel which you don’t want them to ?? since mikrotik source code is not public so we cannot hide that option (as far as in my limited knowledge) . Using the form base technique you can simply give them a web page from where they can simply change there password when required.

You can also add more functions in this page ,like it can send an email or add any entry in log file so that admin can be aware that at which time the last password was changed or other functions as required.

This is a simple password change form for hotspot users, After they logged in to hotspot , they can change there own password using this simple form.

REQUIREMENTS:

– Linux base system (I used UBUNTU, but you can use any flavor of your own choice)
– Apache / PHP5.x / PEAR2 library

Also Make sure you have enabled the API service in MIKROTIK
/ IP > Services
As showed in the image below …

1

LINUX SECTION

First Update your Ubuntu (if its not already updated on fresh installation)

apt-get install update

Now Install Apache Web Server with PHP5

 apt-get install apache2 php5

Don’t forget to restart the apache2 service, otherwise when you will try to open the password change form, it will ask you to save the file, instead of opening it on the browser 😀

service apache2 restart

Now we have to download PEAR2 support library for the RouterOS functions to be performed via WEB,
Goto your web folder and download pear2 library, and extract it

cd /var/www
 wget http://wifismartzone.com/files/linux_related/pear2.tar.gz
 tar zxvf pear2.tar.gz

Ok now it’s time to create the change password page so that user can access it or you can link it with your status page for the user comfort level.

touch /var/www/changepass.php
 nano /var/www/changepass.php

and paste the following code.
{Make sure to change the IP address of Mikrotik and its admin ID Password}

<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$errors = array();

try {
    //Adjust RouterOS IP, username and password accordingly.
    $client = new RouterOS\Client('192.168.30.10', 'admin', 'admin');

    $printRequest = new RouterOS\Request(
        '/ip hotspot active print',
        RouterOS\Query::where('address', $_SERVER['REMOTE_ADDR'])
    );
    $hotspotUsername = $client->sendSync($printRequest)->getArgument('user');
} catch(Exception $e) {
    $errors[] = $e->getMessage();
}

if (isset($_POST['password']) && isset($_POST['password2'])) {
    if ($_POST['password'] !== $_POST['password2']) {
        $errors[] = 'Passwords do not match.';
    } elseif (empty($errors)) {
        //Here's the fun part - actually changing the password
        $setRequest = new RouterOS\Request('/ip hotspot user set');
        $client($setRequest
            ->setArgument('numbers', $hotspotUsername)
            ->setArgument('password', $_POST['password'])
        );
    }
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Change your hotspot password sample page in PHP / Syed Jahanzaib.PK-KHI</title>
        <style type="text/css">
            #errors {background-color:darkred;color:white;}
            #success {background-color:darkgreen:color:white;}
        </style>
    </head>
    <body>
        <div>
            <?php if (!isset($hotspotUsername)) { ?>
            <?php } else { ?>
<h3>
<pre><span style="color: blue">PA</span><span style="color: red">KI</span><span style="color: purple">ST</span><span style="color: orange">AN</span> <span style="color: green">ZINDABAD</span> ...JZ!!</pre>
<h2>
<br>HOTSPOT ... Sample password change FORM <br><br>
You are currently logged in as "<?php
                    echo $hotspotUsername;
                ?>"</h2>

            <?php if(!empty($errors)) { ?>
            <div id="errors"><ul>
                <?php foreach ($errors as $error) { ?>
                <li><?php echo $error; ?></li>
                <?php } ?>
            </ul></div>
            <?php } elseif (isset($_POST['password'])) { ?>
            <div id="success">Your password has been changed.</div>
            <?php } ?>

            <form action="" method="post">
                <ul>
                    <li>
                        <label for="password">New password:</label>
                        <input type="password" id="password" name="password" value="" />
                    </li>
                    <li>
                        <label for="password2">Confirm new password:</label>
                        <input type="password" id="password2" name="password2" value="" />
                    </li>
                    <li>
                        <input type="submit" id="act" name="act" value="Change password" />
                    </li>
                </ul>
            </form>
            <?php } ?>
        </div>
    </body>
</html>

Now once the user have logged in to hotspot, he can access the page like below.

http://192.168.30.50/changepass.php

As showed in the image below …

changepass

.

.

log

Credits and legal stuff

Author: Vasil Rangelov, a.k.a. boen_robot (boen [dot] robot [at] gmail [dot] com)

Regard’s
Syed Jahanzaib

14 Comments »

  1. muy interesante esta informacion, me gustaria si tienes mas ejemplos sobre el manejo de esta api para el manejo de mikrotic .
    muchas gracias.

    Like

    Comment by edison caicedo — February 11, 2014 @ 5:33 AM

  2. Thanks for this solution which i was looking from last 2 month
    thanks a lot

    Like

    Comment by Abhay Singh — February 11, 2014 @ 9:12 PM

    • Hallo sir ….
      Kia ap mjy es user change password user self setting ky bry my guide kr skty ha.
      Mjy es post ki samhaj nai. Please help me

      Like

      Comment by Shahzad Nasir — February 7, 2015 @ 5:55 PM

  3. Sir Syed Jahanzaib
    Thanks for your this solution, i need one support also we are using mikrotik hotspot with 400 user’s every thing is running fine but when user logged on my hotspot network and start browsing , at the time of browsing and download they get ping rto and heavy latancy with packet loss.

    when they try to ping local ip of serfver hotspot port and wan ip of ISP

    Like

    Comment by Abhay Singh — February 11, 2014 @ 9:17 PM

  4. Sir Syed Jahanzaib
    Thanks for your this solution, i need one support also we are using mikrotik hotspot with 400 user’s every thing is running fine but when user logged on my hotspot network and start browsing , at the time of browsing and download they get ping rto and heavy latancy with packet loss.

    when they try to ping local ip of serfver hotspot port and wan ip of ISP

    we are using user manager to make profile

    Like

    Comment by Abhay Singh Sengar — February 11, 2014 @ 9:18 PM

  5. Thanks to your message I want to point out that had the Internet system and work to manage it by usermanager And I have customers – and I need to put a cash balance of each customers and the customer is used to activate the users through that Is it possible in this case, please explain programmatically – as a payments thank you and forget me abute my bad language

    2014-02-10 9:18 GMT+03:00 Syed Jahanzaib Personnel Blog to Share Knowledge

    Like

    Comment by salam thyab — March 6, 2014 @ 4:33 AM

    • User Manager donot support cash deposit in customer/dealer account which can be deducted. You can use linux RADIUS MANAGER as a billign system for your mikrotik which have so many feature, but its not free. for more info you can send msg men on skype.

      Like

      Comment by Syed Jahanzaib / Pinochio~:) — March 6, 2014 @ 8:47 AM

      • syed bhai i want html code to embeed on my login page for users to check their profile .ie 1.plan they have taken 2.data usage left i.e if they have taken 1 gb per month then how many days eft and how much data left. thanks
        i want to add this on login page plus status page

        also when anybody is trying to log in then invalid username or password comes on main page its too small i want to make it bigger so that even a expired user tries to log in he will come to know that his account has been exhausted ….

        please reply me on ayubkhan78666@gmail.com.

        Like

        Comment by ayub khan — November 16, 2014 @ 11:08 AM

  6. i am running user manager and hotspot to control my LAN. is they any file i can edit to allow user to change passwords on first login

    Like

    Comment by gidza — August 1, 2014 @ 5:28 PM

  7. oto cu

    Hotspot User Change Password FORM for ‘User Self Management’ | Syed Jahanzaib Personnel Blog to Share Knowledge !

    Like

    Trackback by oto cu — October 17, 2014 @ 9:51 AM

  8. hi
    Script not working. The following message is displayed.

    Deprecated: Response::getArgument() is deprecated in favor of Response::getProperty() (but note that Request::getArgument() is still valid) in /home/PEAR2/Net/RouterOS/Response.php on line 299

    Like

    Comment by mobinsaffarpour — March 10, 2020 @ 1:11 AM

  9. hello. can i do this with windows os?

    Like

    Comment by eli — December 18, 2023 @ 12:11 PM


RSS feed for comments on this post. TrackBack URI

Leave a comment