Reputation: 26
I created an ssh connection using the following code.
$connection = ssh2_connect($masterIp, 22);
// authenticating the remote server connection with credentials
ssh2_auth_password($connection, $user, $password);
// secure ftp connection to create directory if not exist
$sftp = ssh2_sftp($connection);
After completing the operations I need to close $connection, $sftp. How can I safely close the connections. Can I use unset() function?
Upvotes: 1
Views: 4508
Reputation: 94
If you want to make sure that your SSH session is properly closed consider using phpseclib, a pure PHP SSH implementation. It sends all the SSH packets necessary to close the packet with the destructor and if you don't want to use that has a $ssh->disconnect().
Makes for much more portable code too.
Upvotes: 2
Reputation: 198119
The SSH connection is opened until closed:
ssh2_exec($connection, 'exit;');
There is no explicit ssh2_close
command.
The chances are good that unset
will terminate the network connection as well. At least when the PHP process ends, the connection should get automatically closed.
Upvotes: 1
Reputation: 33457
Yes, unset should suffice.
In PHP, when resources go out of scope or become unset, they are automatically closed (they should be anyway, unless the extension breaks the standard PHP model).
(Well, it will only be garage collected and thus closed when all references to the resource are destroyed)
Upvotes: 3