我有一个简单的简单问题! :-D
我是Php
的新手,我写了一个简单的code
,因此我可以通过SSH
连接到设备。
但是当我在一个类和方法中尝试这个时,不起作用。
这是我的主要Php的代码,也包括我的课程:
<?php
class Connection {
public $ip;
public $usernam;
public $password;
public $ssh;
public function sshConnection() {
include ('./view/login.html');
include('Net/SSH2.php'); // I use phpseclib to connect via SSH
if(isset($_POST['login'])) { // Login button in html file
$this->ip = $_POST['ip']; // input type to get ip in html file
$this->usernam = $_POST['username']; // input type to get username in html file
$this->password = $_POST['password']; // input type to get password in html file
$this->ssh = new Net_SSH2($ip);
if (!$ssh->login($username, $password)) {
print('Login faild');
} else {
echo $ssh->exec('pwd');
}
}
}
}
$connection=new Connection();
$connection->sshConnection();
我所包含的html
文件登录。html
是包含内部CSS
的登录页面的视图。
如您所见,我已通过此代码创建了一个类:
$connection=new Connection();
$connection->sshConnection();
当我使用此代码而不创建类时,它工作正常! 当我定义类时它不起作用,按下Login Button后没有任何反应。
我做错了什么? 任何帮助将不胜感激。 : - )
答案 0 :(得分:0)
所以,正如我在评论中所说,你的代码应该是这样的(我添加了一点奖励 - isset()可以在单个调用中检查很多变量):
<?php
class Connection {
public $ip;
public $username;
public $password;
public $ssh;
public function sshConnection() {
include ('./view/login.html');
include('Net/SSH2.php'); // I use phpseclib to connect via SSH
if(isset($_POST['login'], $_POST['ip'], $_POST['username'], $_POST['password'])) { // Login button in html file
$this->ip = $_POST['ip']; // input type to get ip in html file
$this->username = $_POST['username']; // input type to get username in html file
$this->password = $_POST['password']; // input type to get password in html file
$this->ssh = new Net_SSH2($this->ip);
if (!$this->ssh->login($this->username, $this->password)) {
print('Login faild');
} else {
echo $this->ssh->exec('pwd');
}
}
}
}
$connection=new Connection();
$connection->sshConnection();
干杯!
答案 1 :(得分:0)
感谢我的朋友@arbogastes,我的问题非常简单! 我忘了为我定义的所有变量添加$ this。 还要感谢@Echoes和@zenwraight。 所以我的代码就这样了:
<?php
class Connection {
public $ip;
public $usernam;
public $password;
public $ssh;
public function sshConnection() {
include ('./view/login.html');
include('Net/SSH2.php'); // I use phpseclib to connect via SSH
if(isset($_POST['login'])) { // Login button in html file
$this->ip = $_POST['ip'];
$this->usernam = $_POST['username'];
$this->password = $_POST['password'];
$this->ssh = new Net_SSH2($this->ip);
if (!$this->ssh->login($this->username, $this->password)) {
exit('Login Failed');
print('Login faild');
} else {
echo $ssh->exec('pwd');
}
}
}
}
$connection=new Connection();
$connection->sshConnection();
?>
答案 2 :(得分:-1)
试试这个:
<?php
class Connection {
public $ip;
public $usernam;
public $password;
public $ssh;
public function sshConnection($ip, $username, $password) {
include ('./view/login.html');
include('Net/SSH2.php');
$this->ip = $ip; // input type to get ip in html file
$this->usernam = $username; // input type to get username in html file
$this->password = $password; // input type to get password in html file
$this->ssh = new Net_SSH2($this->ip);
if (!$this->ssh->login($this->usernam, $this->password)) {
print('Login faild');
}
else {
echo $this->ssh->exec('pwd');
}
}
}
if(isset($_POST['login'])) {
$connection=new Connection();
$connection->sshConnection($_POST['ip'], $_POST['username'], $_POST['password']);
}
?>