【发布时间】:2014-09-12 23:28:05
【问题描述】:
在处理ssh2_auth_password() 函数(以及ssh2_connect())上的错误时,我有点挣扎。我成功地连接并登录到我的远程服务器,但是,当涉及到提供错误的凭据时,我希望能够处理这些情况。我正在使用 ZF 1.12,并且我已经创建了一个小控制器插件:
<?php
class My_Controller_Plugin_Scp extends Zend_Controller_Plugin_Abstract
{
private $host;
private $login;
private $password;
private $destDir = '.';
private $connexion;
public function __construct()
{
$co = ssh2_connect($this->getHost(), 22);
if(false === $co)
throw new Exception('Can\'t connect to remote server');
$this->setConnexion($co);
}
/**
* @return bool
* @throws Exception
*/
public function auth()
{
$auth = ssh2_auth_password($this->getConnexion(), $this->getLogin(), $this->getPassword());
if($auth === false)
throw new Exception('Authentication failed!');
return $auth;
}
我在我的控制器中使用这个插件是这样的:
try {
// Préparation à la copie en SSH du fichier template
$scp = new My_Controller_Plugin_Scp();
$scp->auth(); // Authentification
} catch(Exception $e) {
echo $e->getMessage();
$this->getHelper('Redirector')->setGotoRoute(array(), 'ficheVoir');
exit();
}
实际输出是,当我弄乱凭据时,会引发 PHP 警告,而我希望能够显示自定义错误消息。
Exception 之类的东西我做错了吗?
【问题讨论】:
-
错误信息是什么意思?
-
它按预期显示
Warning: ssh2_auth_password(): Authentication failed for login using password,因为我提供了错误的凭据。 -
这可能很愚蠢,但尽量不要将返回值分配给
$auth变量并从if调用函数。另外,请尝试使用 @ 抑制错误。 -
我已经尝试不将
$auth分配给return并使用@抑制错误,但我留下了一个空白页,而不是转到catch()表达式。跨度>
标签: php zend-framework ssh exception-handling basic-authentication