6

how do we connect to a remote server via sftp to verify if the login details are valid in php...

i'm using apache server... my use is to check whether the login details entered by user is correct or not.

Akhil K Nambiar
  • 3,835
  • 13
  • 47
  • 85

4 Answers4

4

Do you have an apache server installed? for example: xampp?

If you do then you have use the FTP function:

<?php
$connection = ssh2_connect('ftp.server.com', 22); //port 22 for Shell connections
ssh2_auth_password($connection, 'username', 'password');

$shell_ftp = ssh2_sftp($connection);

$connectionStream = fopen('ssh2.sftp://'.$shell_ftp.'/path/to/fileorfolder', 'r');

if(!connectionStream)
{
    echo "Could not connect to the server, please try agian";
}
else
{
    echo "Successfully logged in.";
}
?>

That the basic Shell FTP connection, you must define absolute path's to file's or folders.

Wesley
  • 798
  • 3
  • 8
  • 15
  • This is better than my answer. – Elzo Valugi Dec 07 '10 at 12:12
  • how do u know that the connection is successfull or not... thr wont be any file operations... only need to verify user name and password... – Akhil K Nambiar Dec 07 '10 at 12:14
  • i don't need this line $connectionStream = fopen('ssh2.sftp://'.$shell_ftp.'/path/to/fileorfolder', 'r') also for testing I'm using a server with port number 2275. will that be a problem. cause it gives error Error starting up SSH connection(-1): Failed sending banner – Akhil K Nambiar Dec 07 '10 at 12:36
  • Well, if you want to check if the login is valid. then you will be needed that line to check if the $connectStream is open or closed, also it would be better to use port 22 for shell. – Wesley Dec 07 '10 at 14:10
3

You might have an easier time using phpseclib, a pure PHP SFTP client. Here's an example:

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>

The problem with libssh2, as everyone's recommending, is that it's not very widely deployed, it's API isn't exactly the most reliable, it's unreliable, poorly supported, etc.

wassail
  • 46
  • 1
2

will this help?

<?php
$connection = ssh2_connect('ftp.server.com', 22); 
if (ssh2_auth_password($connection, 'username', 'password')) 
    echo "Authentication success";
else 
    echo "Authentication failure";
?>
viMaL
  • 638
  • 1
  • 7
  • 20
0

I formed a small lib out of multiple suggestions. I encour

idct sftp client on GitHub

Bartosz Pachołek
  • 1,278
  • 1
  • 8
  • 17