И вот как это работает: сначала нужно аутентифицировать пользователей точки доступа и предоставить им доступ в интернет… После регистрации пользователь может вернуться на страницу входа и войти, используя свое новое имя пользователя и пароль. Как уже говорилось, вы не можете автоматически авторизовать их сами. Они должны сделать это самостоятельно. "Позвольте им выбрать желаемое имя пользователя и проверьте, существует ли оно" — как это сделать? Вы отправляете запрос "print" с запросом и проверяете, возвращается ли ноль. Если да, то пользователя не существует. С точки зрения протокола это выглядит так: /ip/hotspot/user/print
=count-only=
?name=usernameToBeChecked, где “usernameToBeChecked” – желаемое имя пользователя, и в ответе !done
=ret=1 (если “ret” равно “1”, имя пользователя существует, а если “0”, то нет). Используя пакет PHP, это будет выглядеть так: <?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';
if (isset($_GET['act'])) {//This is merely to ensure the form was submitted.
$errors = array();
try {
//Adjust RouterOS IP, username and password accordingly.
$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
} catch (Exception $e) {
$errors[] = 'We are sorry, but we are unable to register you at this time. Please try again later.';
}
//This is just one approach that allows you to create a multi purpose form,
//with ping being just one action.
if ($_GET['act'] === 'reg') {
if (!isset($_POST['username'])) {
$errors[] = 'You must provide a desired username.';
}
if (!isset($_POST['password'])) {
$errors[] = 'You must provide a password.';
}
if (!isset($_POST['password2'])) {
$errors[] = 'You must confirm your password.';
}
if ($_POST['password'] !== $_POST['password2']) {
$errors[] = 'Passwords do not match.';
}
if (empty($errors)) {
//We are connected to the router and we have a valid username and password.
//Check for existence of user NOW, and register them if they don't exist.
$printRequest = new RouterOS\Request(
'/ip hotspot user print count-only=""',
RouterOS\Query::where('name', $_POST['username'])
);
if ($client->sendSync($printRequest)->getArgument('ret') != '0') {
$error[] = 'The desired username is already taken.';
}
if (empty($errors)) {
$addRequest = new RouterOS\Request('/ip hotspot user add profile=profile1');
$addRequest
->setArgument('name', $_POST['username'])
->setArgument('password', $_POST['password']);
if (count($client->sendSync($addRequest)) > 1) {
$error[] = 'We failed to register you for some unknown reason. Please contact us about it.';
}
}
}
}
}
?><!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>User registration</title>
<style>
#errors {background-color:darkred;color:white;}
#success {background-color:darkgreen;color:white;}
</style>
</head>
<body>
<div>
<?php
if (isset($_POST['act'])) {//There's no need to execute this if the form was not submitted yet.
if (empty($errors) {
echo '<div id="success">You are now registered!</div>';
} else {
echo '<div id="errors">There were errors when trying to register you:
';
foreach ($errors as $error) {
echo '- ', $error '';
}
echo '
</div>';
}
}
?>
<form action="" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="<?php
if (isset($_POST['username'])) {
echo htmlspecialchars($_POST['username']);
}
?>" />
<label for="password">Password:</label>
<input type="password" id="password" name="password" value="" />
<label for="password2">Confirm password:</label>
<input type="password" id="password2" name="password2" value="" />
<input type="submit" id="act" name="act" value="reg" />
</form>
</div>
</body>
</html> (P.S. A few of the critical lines will not be needed in the upcoming version, in favor of more compact one liners, but this is what you can use today with the exact same effect, and which will still work the same then as well)