請注意,當驗證失敗時,ssh2_auth_password 函式會產生 PHP 警告 (!) 。若要避免警告,請使用「靜默」("@") 運算子。
<?php
$ssh = ssh2_connect($host);
if (false === $ssh) {
die('連線失敗');
}
$auth = @ssh2_auth_password($ssh, $user, $password);
if (false === $auth) {
die('驗證失敗');
}
?>
(PECL ssh2 >= 0.9.0)
ssh2_auth_password — 使用純文字密碼透過 SSH 進行驗證
使用純文字密碼透過 SSH 進行驗證。自 0.12 版以來,此函式也支援 keyboard_interactive 方法。
範例 #1 使用密碼進行驗證
<?php
$connection = ssh2_connect('shell.example.com', 22);
if (ssh2_auth_password($connection, 'username', 'secret')) {
echo "驗證成功!\n";
} else {
die('驗證失敗...');
}
?>
請注意,當驗證失敗時,ssh2_auth_password 函式會產生 PHP 警告 (!) 。若要避免警告,請使用「靜默」("@") 運算子。
<?php
$ssh = ssh2_connect($host);
if (false === $ssh) {
die('連線失敗');
}
$auth = @ssh2_auth_password($ssh, $user, $password);
if (false === $auth) {
die('驗證失敗');
}
?>