2017-05-17 10 views
0

私は簡単な質問があります! :-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ファイルログイン: は、ここでも私のクラスを含み、メインのPHP私のコードです。私が含まれているhtmlは、いくつかの内部のCSSを持つログインページのビューです。 あなたは私は、このコードによってクラスを作成している見ての通り:

$connection=new Connection();  
$connection->sshConnection(); 

私はそれが正常に動作するクラスを作成せずにこのコードを使用すると! 私はクラスを定義するので、私はログインボタンを押しても何も起こらず、何も起こらない。

ここで私は間違っていますか? 助けていただければ幸いです。 :-)

+1

は、なぜあなたは 'の場合(ISSET($ _ POST [ 'ログイン'])){'内部クラス/関数を使用していますか? クラス外でも使用していますか? – Echoes

+1

クラス外にインクルードステートメントを置くことができます。 – zenwraight

+0

@Echoes:いいえ、クラス外では使用しません。 クラスのすぐ内側です。 –

答えて

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

はこれを試してみてください:私はコメントで言ったように

<?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']); 
} 

?> 
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(); 
?> 
関連する問題