SFTP not connecting to remote server - php

I am trying to connect to a remote SFTP server using PHP. My code works fine when I connect to a local SFTP account but it times out for remote host. I have made sure through FTP client that host information is correct and its connecting fine.
I am using phpseclib library and my three line code is below.
require_once("phpseclib/Net/SFTP.php");
$sftp = new Net_SFTP('remote_host_IP');
var_dump($sftp->login('<username>', '<password>'));
It returns false (meaning not connected).
What I have done
I have whitelisted script in mod_security just in case its blocking that.
I have tried same script on my local computer and it connects successfully to remote SFTP.
Any valuable hint please?

Do define('NET_SSH2_LOGGING', 2) before initializing Net_SFTP and then do $sftp->getLog() after $sftp->login()
That'll provide enough info with which a diagnostic can be made.

This might help others. You need to make sure TCP_OUT port is open on your remote server to make it work.
Thanks for the help!

Related

PHP cannot connect to MySql database on live web server

$con=mysqli_connect('localhost:3306','Punya','password','saain');
My PHP website, which is live is unable to connect to database. It works fine on my Pcs local server. When I write localhost:3306, it executes the echo statement I wrote for debugging.
If I write just localhost, it gives:
http 500 error
.
Cannot say much without knowing the actual error. But I could assume a few things based on what you have given.
When you pass localhost as the hostname to the mysqli_connect() function, it tries to create the connection using a Unix socket instead of TCP/IP. So it seems like either you don't have permissions to access the socket, or you need to get the correct socket information from your hosting provider.
Also, when you pass it as localhost:3306, you force it to connect via TCP/IP instead of socket. That's why it works.
However, you can force mysqli_connect() function to connect via TCP/IP by providing the hostname as 127.0.0.1.
$con = mysqli_connect('127.0.0.1','Punya','password','saain');
If this doesn't work, I assume they have defined a different default port for mysql in the php configuration. You can see it by printing it like:
echo ini_get("mysqli.default_port");
In case this default port is different from 3306 (which is unlikely), you can pass the port as the fifth paramter to mysqli_connect() function like this.
$con = mysqli_connect('127.0.0.1','Punya','password','saain', 3306);
You need to contact your hosting provider to get more details on this. Until you do that, your solution is to connect as shown above.
I hope you understand why it happens :) Feel free to ask if you have any doubts.
Try to check in
check here
Synax errors
check in your 👇
file.php
Check what version your live server is running and what version our location machine is running. most of the time environmental factors make these types of errors. I'm going to assume your hosting environment is a shared hosting for this answer
There are couple of things you can do identify the problem first.
Enable Errors
if you are hosted on a cPanel shared hosting server I doubt you can change the php.ini file. what you can do is enable PHP errors on runtime so you will have a proper error than just a 500 error. put the below codes in the top of your PHP file
<?php
error_reporting(E_ALL);
ini_set("display_errors", "On");
PHP info file
create a php.info file to identify what sort environment you are hosting your website on.
<?php
phpinfo();
?>
Cpanel has there own MYSQL server domain and port. this information you should get when you are creating a database in cPanel.
Once you have the error. paste it here we might be able to give you a better answer how to fix it

Cannot connect to server from another server ftp php

I work with 2 servers one is my production server other is my resource server.
I cannot connect to my resource server from my production server over ftp.
I can connect to other servers from my production server.
I can also connect to my resource server from my localhost or filezilla.
I use this code to connect :
$conn_id = ftp_connect("resource server ip", "21", "5");
if ($conn_id) {
echo "connected";
ftp_close($conn_id);
}
print_r(error_get_last());
I don't get any output when I run this script on server(no error).
On localhost it runs no problem.
What can be the problem with this? Is this something that server admin has to resolve? Thanks for help.
You should first check from commandline, whether it's a networking/OS issue or not.
So if you've got shell access to the production server try connecting to the resource server via the commandline ftp client.
If that does not work, you've got a network / firewall / access control problem, not related to php or your software, and you should talk to the sysadmin.
If it does work, then the problem is in your stuff, and you should set the log levels to high, and run this script from commandline, also check the logs of php, php-error, syslog and the resource servers ftp access log and syslog too.
Note: ftp is a not-too-exact beast, the servers and clients have a lot of workarounds built in to treat each other in a way, that works somehow. There could be issues from active (multiple back-and-forth connections) and passive mode (it's like http), also with ls formats and timestamps, timezones and ports.
Also some servers only support ftps (ftp with ssl) - which is not the same as sftp (file transfer over ssh - port 22).
Your production server probably has some firewall rules, and your connection get caught on that, to debug this, please use the commandline ftp client, and/or nmap / netcat.

Connection time out while reading contents from a server

Hi Here's what i am trying to do .
$contents = file_get_contents('http://123.34.56.123:3000/?location=Example'); //Not the original ip
//Node script running on port 3000
Everything is working fine on localhost and when i access this url directly from browser but on my web server i ma getting this error
file_get_contents(http://123.34.56.123:3000/?location=Example) Failed to open stream: Connection timed out in ....
And i have tried connecting with curl , sockets and file stream but everything getting the connection is timing out though it's working fine on my local server
I have also used ini_set('max_execution_time', 300); but it didn't help anything
Note : file_get_contents('http://www.google.com'); Works fine on the web server
Anyone can help me understand why i am getting this error ? i have tried almost everything i could to fix this issue.
First of all you need to check that whether the content which you are fetching from resource is encoded or not, means if the content is encoded with JSON then you need to use json script to decode it and after that you will be able to be get the content..
also check the resource URL and the port of the server that you are using.
If your port 3000 is blocked then it also not works fine.
I though it will help you.
Seems like the server i was using was blocking connection on port 3000 , and it makes complete sense as normally web host use 80 and 443 port only to make i/o connection . I contacted with my web host they unlocked the 3000 port and it's working perfectly now .

Php ftp_connect fails, FTP client connects

(this question might not belong here, but on ServerFault, I don't know exactly where the problem comes from)
I'm trying to connect to an FTP server like this:
$con = ftp_connect( '86.xxx.xx.xxx', 21 ) or die("FTP connect error");
and it always throws me the error message.
I can connect like this to other FTP servers, but not this one. I can also connect to this one via an FTP client.
What server settings could be wrong that permit a client connect but not PHP?
Thanks for any help!
It seems my host was blocking remote FTP access to other servers. Oddly enough it allowed me to connect to my other servers at this host.

PHP ftp_connect() returns false always except with first used ftp server..!

The problem is ..
I just used ftp_connect() for the first time on my server
<?php
$ftp_connect = ftp_connect('ftp.server.com') or die('<b>FTP Connection Error ...</b>');
$login_result = ftp_login($ftp_connect, 'ftp_username','ftp_password') or die('<b>FTP Login Error ...Check Your UserName and Password</b>');
ftp_pasv($ftp_connect,true);
?>
It worked fine and returned true.Every thing works fine with that ftp server (ftp.server.com)
Then next time I used the same server and everything was fine...BUT
when I tried with another server (say: ftp.server2.com) it returned false,i.e host not found ..!!
I tried with all my friends ftp servers and nothing could connect except that one(ftp.server.com)
what's actually going on ... why is this happening and how can I solve this ... right now i can't connect any ftp server (except my ftp.server.com) ....
Thanx in advance ...
And The above thing works fine on my pc (IIS,PHP 5.3.8)
Updated :
ahhh ....... figured out the problem .... but didn't find a solution.!!!!
the server (here: ftp.server.com) has the same IP which the host server, where the above php is hosted - has.... and soo the ftp_connect() worked on it....
reason is both the servers ftp and http host are from same web hosting provider...
Now its clear, (I think) that firewall is the main reason for all the problem .... how to fix that firewall...?? plz help.... Thanx again..
You are not specifying a port number. PHP's ftp_connect() will use 21 as the default port number if you don't specify one. This script may have worked for the first server because it was running on port 21, but the other servers may be failing because they're running on different ports. Ask your friend(s) what port they're using, or use something like nmap to find it yourself.

Categories