Change Hotspot Account With PHP-API Mikrotik

Mikrotik has a feature called API (Application Programmable Interface) which allows us to make or create an interaction with mikrotik routerboard from our application rather than using CLI (Command Line Interface) directly by mikrotik terminal.

So, in this time, I'm showing you how to give the hotspot users a discretion to managed their hotspot password. This will be good if you have fixed users who are using your hotspot service, so they can change their password by them self rather contact you to change their password.

First of all, you have to enable API service on your routerboard in IP -> Services. There are two type of api are API and API-SSL. API-SSL has an ability to encrypt your connection between your application and routerboard. But, if your application has been implementing HTTPS-SSL, this is not needed anymore. So, please make sure your configuration is same like this below picture :

After that, you have to download routeros_api.php plugin and should be inputed in one directory with your PHP application.

Then create a  PHP file named index.php and copy/paste this below code into it :

<html>
<head>
</head>
<body bgcolor="#0e0b50">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td>
<form name="form1" method="post" action="change_password.php" >
<table width="300" align="center" border="2" cellspacing="1" cellpadding="1" bgcolor="gray" height="213">
<tr>
<td>
<table width="100%" align="center" border="0" cellspacing="0" height="171">
<tr>
<td colspan="3" align="center" height="49">
<strong>User Hotspot Change Password </strong>
</td>
</tr>

<tr>
<td width="34%">User ID</td>
<td width="3%">:</td>
<td width="54%"><input name="myusername" type="text" /></td>
</tr>

<tr>
 <td width="34%">Old Password</td>
 <td>:</td>
 <td><input name="oldpassword" type="password" /></td>
</tr>

<tr>
 <td width="34%">New Password</td>
 <td>:</td>
 <td><input name="newpassword" type="password" /></td>
</tr>

<tr>
 <td colspan="3" align="center" valign="bottom">
 <input type="submit" name="s1" value="Change Password" /> 
 <input value="Cancel" type="reset" name="r1" />
 </td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td height="50">&nbsp;</center></td>
</tr>
</table>
</body>
</html>

So, the result will be like this :

Then create another PHP file named change_password.php to execute inputed data and handle it then be sent into mikrotik routerboard.


<?php
error_reporting (E_ALL ^ E_NOTICE); 
$proses = $_POST["s1"];
$ip = 'your_IP_Address';
$old_name = $_POST["myusername"];
$old_password = $_POST["oldpassword"];
$new_password = $_POST["newpassword"];
$cmmnd ="=password=";
$cmmnd .=$new_password;

if(!empty($proses))
{
if ((!empty($old_name)) && (!empty($old_password)) && (!empty($new_password)))
{
require('routeros_api.php');

$API = new routeros_api();
$API->debug = false;


if ($API->connect($ip, 'your_mikrotik_username', 'your_mikrotik_password'))
{
$API->write('/ip/hotspot/user/getall',false);
$API->write('?name='.$old_name);
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
$NILAI = $ARRAY[0][password];
if($old_password <> $NILAI)
{
echo '<script>alert("Your password or USER ID is wrong."); window.location.href = "https://www.google.com";</script>';
} 
Else 
{
$API->write('/ip/hotspot/user/set',false);
$API->write('=.id='.$old_name ,false);
$API->write('=name='.$old_name ,false);
$API->write($cmmnd);

$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
echo '<script>alert("Password changed successfully!"); window.location.href = "https://www.google.com";</script>';
}
$API->disconnect();
}
else
{
  echo '<script>alert("Server not found! Please contact your IT administrator."); window.location.href = "https://www.google.com";</script>';
  $API->disconnect();
}
}
else
{
echo '<script>alert("You did not input any data."); window.location.href = "https://www.google.com";</script>';
}
}
?>

That's all my explanation to you and you may improve or add other feature to this application to control your mikrotik routerboard.

Hope this will help you. Cheers :)

Comments

  1. untuk yang add user gimna mas , ada kah sourcenya

    ReplyDelete
    Replies
    1. Tinggal di modifikasi saja perintah routerOSnya.
      Kalau ingin lengkapnya, silahkan didownload dan dicoba script di bawah ini :

      https://drive.google.com/file/d/0B0eiI_3d7A65OXo2TTJMd0l5aFU/view?usp=sharing

      Delete
  2. mas bisa gak login page usernamenya di ganti dengan email , jadi dia yang ngasi emailnya , dari pihak kita cuma kasi password , jadi dari pihak kami biar bisa kirim penawaran ke email yang di dapetin tadi

    ReplyDelete
    Replies
    1. Bisa saja seperti itu dan harus disediakan database server untuk menampung data email user. Jadi konsepnya saya rasa seperti ini :

      1. User memasukan email pada form login wifi jika dia belum teregistrasi.
      2. Di form tersebut terdapat kode PHP untuk menginputkan data email ke database server dan jika mau, script PHP di halaman tersebut menggenerate password otomatis untuk user.
      3. Jika sudah berhasil masuk ke database, ada halaman admin untuk mengirimkan email ke user untuk menginformasikan username dan password dan mungkin beserta kontent marketting yang mau ditawarkan. Atau juga bisa menggunakan script PHP untuk mengirimkan email otomatis ke setiap user yang baru saja teregistrasi. untuk mengirimkan email melalui PHP, silahkan cari di google saja soal PEAR Mail.
      4. Setelah email berhasil terkirim, akan mengtriger script PHP lain yang menggunakan router_os API untuk menginputkan username password milik user baru.

      Semoga membantu.

      Delete
  3. Nice tutorial!
    Could you show us how to add profile to existing user?
    Thank you in advance!

    ReplyDelete
    Replies
    1. Thank you for visiting my Blog.

      I'm writting the tutorial how to add existing user with registration form. Please wait. :)

      Delete
  4. Pengen buat juga, tapi routernya belum punya. Hehe. Makasih atas ilmunya.

    ReplyDelete
    Replies
    1. Makasih, Mas sudah mampir.

      hehe.. kalo dirumah ada internet+mikrotik, bisa itu mas bikin hotspot sendiri.
      Kalo mau dikomersialkan kembali juga bisa. hehe.. :D

      Delete
  5. Hi Kevin,
    In php how can I assign bandwidth of upload/downloadfor one user?
    also, how can I assign the session time to use internet?
    Como serĂ­a en PHP para asignar ancho de banda de subida y bajada para un usuario?, y como asignar el tiempo de uso del internet?


    Thanks

    ReplyDelete
    Replies
    1. Hi Amarante, PHP can only sends commands to mikrotik router and doesn't manage/limit the bandwidth directly (This is what I got from your question). But, if you want to assign and manage bandwidth and session per user, it is too complicated to be implemented because you gonna typing all mikrotik command including php code it self and also you have to code a PHP where listening of every assigned hotspot user. Better you manage the hotspot users and the bandwidth directly from your router. You can read my article about managing the bandwidth : http://www.revo-brain.com/2015/08/simple-queue-bandwidth-management-in-mikrotik.html

      Delete
  6. How to add and implement this code in "forgot Password" link?

    ReplyDelete
  7. saya mau tanya mas admin, di kafe tempat saya bekerja mau dibuat login hotspot, dengan cara melakukan registrasi terlebih dahulu.dengan memasukkan nama email ,telepon dan alamat , setetelah memasukkan registrasi tersebut ,baru bisa terkoneksi ke internet dengan cara memasukkan username dan password yang dibuat tadi . tujuannnya adalah untuk menjaring database customer yang hadir di cafe melalui hotspot.

    apakah dengan API mikrotik bisa diterapkan sistem seperti tersebut?

    langkah apa saja yang perlu dilakukan.

    terima kasih

    ReplyDelete
  8. please make a video of this topic for best help

    ReplyDelete
  9. Hello, thanks for your post... i have a question... as active a previously created user...

    ReplyDelete

Post a Comment

Please leave your comment politely and do not write a spam message.

Thank you. :)