PHP 7 - SFTP keeps interrupting page to load - php

I'm currently trying to get a file from a server by php sftp. I managed to authenticate and connect to the server. The problem is, that if I want to open a dir on said server, the page just keeps loading until my browser tells me the loading of the page has been interrupted. This only happens, if I try open a dir that EXISTS. If I open a dir that doesn't exist, I get a normal error message.
Therefor I'm not quite sure, wether this is a mistake in my code or a problem with the ftp server.
My Code:
ini_set("display_errors", "1");
$host = "<host>";
$port = 22;
$conn = ssh2_connect($host);
$username = "<user>";
$pub_key = "/home/<user>/.ssh/id_rsa.pub";
$pri_key = "/home/<user>/.ssh/id_rsa";
if (ssh2_auth_pubkey_file(
$conn,
$username,
$pub_key,
$pri_key
)) {
if(!$sftp = ssh2_sftp($conn)){
die("SFTP Connection failed");
};
opendir("ssh2.sftp://".intval($sftp)."/./");
};
Has anyone ever experienced something similar?
I'd be glad for any help :)
~François

It is the expected way.
Opendir return a handle. Your function is working, it's just that you do nothing with the data, and your php script do nothing. It's just waiting with the information
Just handle data, or at least write an echo and it should be ok.
check the manual, there is a working example http://php.net/manual/en/function.opendir.php

Related

SFTP Opendir failing when called via Function

I have a bunch of php pages that are running on a schedule that pull data from different SFTP sources, to try and minimise this as a temporary fix I am turning them into functions and having one page that calls each of them
However, when converting these to functions the pages are giving me error 500, through process of elimination I have found that it is when it using the opendir function via sftp
I have called the function page by itself with a reference to call itself as a test and it connects fine, but when called from another page it errors out.
The variables being used to open the directory via sftp are generated on the 'required' page so it's not losing anything via session variables
If I point it to an incorrect directory it is being caught by the error handling in place, but when it 'successfully' connects I get the error 500
Page1.php
<?php
require 'Page2.php';
sftpFunc();
?>
Page2.php
<?php
Function sftpFunc()
{
/* variable declarations and value assignments go here*/
if (!$FTP_CONN = ssh2_connect($FTP_HOST, $FTP_PORT))
die('Unable to connect');
if (!ssh2_auth_password($FTP_CONN, $FTP_USER, $FTP_PASS))
die('Unable to authenticate.');
if (!$FTP_STRE = ssh2_sftp($FTP_CONN))
die('Unable to create a stream.');
if (!$FTP_OPEN = opendir("ssh2.sftp://{$FTP_STRE}{$FTP_DIRI}"))
die('Could not open the directory');
}
?>
Updated the php version on the site from 7.2.34 to 7.3.11 and it seems to have done the trick, hadn't seen any version specific issues with opendir() before and it seemed to only happen when using it through a function on a different page.
Not sure on the why, but at least this worked.

how to include/require a connection to mysql file from another server with php

i have a connection file
file name inc.server.php
<?php
function db_name() { return 'dbname'; }
function db_user() { return 'username'; }
function db_pass() { return 'pw'; }
$koneknodatabase = mysqli_connect('localhost:2020',db_user(),db_pass(),db_name());
function close_Con() {
mysqli_close(mysqli_connect('localhost:2020',db_user(),db_pass(),db_name()));
}
?>
this file save in the my server with IP : 10.2.60.2
but when i require that file from my local pc
with
require('http://10.2.60.2/inc.server.php');
global $koneknodatabase;
$select = mysqli_query($koneknodatabase,"select from data");
$data = mysqli_fetch_array($select);
iam run that script on my localhost
but the result is
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
i have change allow_url_include to ON from php.ini
please help
You can't access the source code of a PHP script of a remote host like that, otherwise everyone would open the config.php file from every webpage they know (and they know the file exists) and get any login data they want.
When you open the URL http://10.2.60.2/inc.server.php in your browser you see the content someone outside of the host "10.2.60.2" will see. It will be most likely an empty page since your inc.server.php file doesn't generate any output (and shouldn't). So your other PHP host will include just an empty file from his point of view, which means there aren't any functions like db_name() defined or any variables like $koneknodatabase.
There are different ways on how separated hosts can communicate to each other, but thats a different topic and might result in different questions. Luckily there are millions of informations out there about how two hosts can communicate with each other.

Apache2 configuration only execute PHP partially

I am experiencing trouble while installing Apache2 on my Debian 8 VPS (jessie).
I installed the website correctly on Hostinger, it works perfectly, but knowing Hostinger was a free plan, I moved to a sufficient VPS so I could get my hands dirty and do the job my myself. I now can handle everything, but the truth is, it's been a long time since I didn't use a debian server, and installing Apache seems harder than I thought.
So, I secured my VPS as I wanted, that's not the problem, the site works correctly, but partially.
I mean, on somes pages, the PHP code executes very well, it works with my database without any problem. I have a utils.php file that contains a getBDD() function like this one :
return new PDO("mysql:host=localhost;dbname=name;charset=utf8", "user", "password");
And it works on my main pages, I can request everything. You can see it here : http://mdjfeelzor.ovh/petiteancienne.php (the website is french).
But I also have an ajax chat that needs a bit of PHP to work properly. Aaaand I don't understand why, but when I initialize my $conn with getBDD(), I am experiencing an issue further in the code.
Look at that code :
include '../utils.php';
session_start();
if(isset($_POST['enter'])){
if($_POST['name'] != ""){
$_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
try {
$conn = getBDD();
$answer = $conn->prepare('SELECT * FROM openchats WHERE ip=? AND name=?');
$answer->execute(array(stripslashes(htmlspecialchars($_SERVER['REMOTE_ADDR'])),
stripslashes(htmlspecialchars($_SESSION['name']))));
if (!($data = $answer->fetch())) {
$req = $conn->prepare('INSERT INTO openchats (ip, name) VALUES (:ip, :name)');
$req->execute(array(
'ip' => stripslashes(htmlspecialchars($_SERVER['REMOTE_ADDR'])),
'name' => stripslashes(htmlspecialchars($_SESSION['name']))));
}
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
}
}
It works correctly, right ? (it's supposed to work correctly on your machine) But the thing is, the code starts showing on my web page on the first call of prepare() as you can see here : http://mdjfeelzor.ovh/chat
So, I was wondering, what do you think is the problem ? Is it my way of programming that isn't working ? By the way, I'm using PHP5. I also tried the code <?php phpinfo(); ?> it works perfectly.
Thanks for your help !
EDIT : By the way, the code is shown but it is not supposed to enter in the condition, as $_POST['enter'] doesn't exist on the first load, the code really works on my computer and on Hostinger so I think that the problem comes from Apache configuration ^^' and the AJAX code is not running at that point.

remote server does not like whatsapi script line

I have just resolved one whatsapi issue on this forum and now am confronted with another one.
I am running scripts by calling the classes in whatsprot.class.php. I can run all scripts on my local machine like a charm. But remote server doesn't like the a certain line in my script and refuses to go beyond that;
My script is;
require "src\whatsprot.class.php";
$username = "91xxxxxxxxxxx"; //Mobile Phone prefixed with country code so for india it will be 91xxxxxxxx
$password = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$identity = strtolower(urlencode(sha1($username, true)));
$w = new WhatsProt($username, $identity, 'WhatsApp Messaging', true);
The line that my remote server doesn't cross is;
$w = new WhatsProt($username, $identity, 'WhatsApp Messaging', true);
I definitely am an amateur. So do let me know what other info would be required to demystify this issue...
Finally the error logs told me that there was some path issue in the libraries. When I moved my executing file into the "src' directory where whatsprot.class.php was located, voilĂ  the script started working !

file_put_contents stops my code from executing

I want to send some data as a GET request to my php page (submit.php) and save it in a local file. I have:
$loc = 'data.txt';
if(isset($_GET["data"])) {
echo 'set';
file_put_contents($loc, $_GET["data"], FILE_APPEND);
}
echo 'foo';
But when I access submit.php?data=bar, nothing happens to data.txt; moreover, echo 'foo' does not seem to execute. Why is this?
echo 'foo' does not execute because file_put_contents() encounters an error and the execution stop.
Put error_reporting(E_ALL & ~E_NOTICES); ini_set('display_errors', '1'); in front of your script to let PHP display the errors on screen.
This way you can find that, I guess, the process that runs the PHP code (the web server probably) does not have the rights to write in the directory where you store the code.
Change $loc to '/tmp/data.txt' and it will work.
Or, even better, create a new directory, set its permissions to rwx for everybody and change the code to write files in it.

Categories