Community discussions

MikroTik App
 
marchelace
just joined
Topic Author
Posts: 1
Joined: Wed May 30, 2018 5:42 am

"User hotspot change password itself"Mikrotik PHP API

Wed May 30, 2018 6:23 am

<?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.88.1', 'admin', 'password');

$printRequest = new RouterOS\Request(
'/ip hotspot active print .proplist=user',
RouterOS\Query::where('address', $_SERVER['REMOTE_ADDR'])
);
$hotspotUsername = $client->sendSync($printRequest)->getProperty('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</title>
<style type="text/css">
#errors {background-color: darkred; color: white;}
#success {background-color: darkgreen; color: white;}
</style>
</head>
<body>
<div>

<h1>You are currently logged in as "<?php
echo $hotspotUsername;
?>"</h1>

<?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>
here my script,but can't change password
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: "User hotspot change password itself"Mikrotik PHP API

Wed May 30, 2018 9:14 am

What do you mean "can't change password"?

Before all that... Can you successfully open up the web page and see the correct username you're already logged in with? Does the form get submitted successfully, and yet setting the password fail?Are there any error messages at any point? What version of PHP are you using, and are you sure your server's firewall is configured correctly to allow outgoing connections to the router's API port? Does the router user have "write" and "sensitive" permissions?

Who is online

Users browsing this forum: No registered users and 69 guests