Setting PHP on IIS ODBC Connection Timeout - php

I've inherited an issue on a Joomla site running on PHP 5.5 on IIS 7.5. Currently it accesses a SQL server through our firewall as part of a query, but this server may not always be available so I wanted to make sure the script dealt with this gracefully. We connect with
$itemodbc = "Driver={SQL Server Native Client 10.0};Server=$itemserver;Database=$itemdatabase;";
$con = odbc_connect($itemodbc,$itemuser,$itempassword) or die('SQL connection error');
but if I simulate the server being unavailable by changing the server's IP address this takes ages and eventually the page script (I believe) times out and errors. What I want to do is set this timeout to 5 or 10 seconds, and if this times out then I can continue the script only using the server's local database content, but I can't find where to set this up. I've found the mssql.connect_timeout entry in the php.ini but this doesn't make any difference (I'm sure this is for mssql_connect calls, rather than odbc_connect, so this makes sense), and there doesn't seem to be an ODBC equivalent, I've also tried appending "Timeout=10;" to the ODBC connection string, but this also doesn't seem to work. Hunting round google I have found what seem to be the settings for a Linux system, but obvisously that also isn't helpful. Where do I need to set this in Windows, or do I need to convert it to another database connection type?

I imagine you have this solved already, but I would have used some kind of PHP based code to 'test' if the server was online. A quick google showed this might be a possible solution for your problem (just change google.com to the direct route to your server, maybe an IP on a different port, or maybe something like server.companyhost.com)
function availableUrl($host, $port=80, $timeout=10) {
$fp = fSockOpen($host, $port, $errno, $errstr, $timeout);
return $fp!=false;
}
//Return "true" if the url is available, false if not.
if (!availableUrl("www.google.com")) {
print " some message that the server is down with neat html that suits your needs ";
exit;
}else{
//the connection is ok... proceed
}

Related

ftp_rawlist always fails on FTPES server with passive mode

I have to connect to a FTPES server to retrieve data. Connecting and logging in works just fine, but my call with ftp_rawlist always fails and returns "false".
I am using this code for debugging purposes:
$ftp = ftp_ssl_connect($ftp_host);
if (ftp_login($ftp, $ftp_user, $ftp_pass)) {
$p = ftp_pasv($ftp, true);
var_dump($p);
$r = ftp_rawlist($ftp, '/', true);
var_dump($r);
} else {
echo 'Could not login';
}
$p is always true, $r always false.
When I connect to the server through Filezilla everything works fine and I can list directory content and more.
Update #1: Tried to not only list '/' but various subfolders on the server, they all fail through the script.
Update #2: Also tried to use ftp_raw with the commands to get a list, but the LIST command runs for some time and then does not return any result at all. But HELP lists LIST as a valid command for the server... Strange...
Update #3: I tried phpseclib now, but while I can connect, I can't login with the user/password combination. Support from the maintainer of the FTPES server is not happening ("works fine for $somebody else..."), so I need to figure this out another way... :-)
To come to an end with this: As the deadline for this project came closer a solution had to be found. And although this is no real answer in the sense of a question, I'd like to show what I have done to have this fixed. Maybe someone stumbles upon this through googling.
Next to the things mentioned in the OP, I also tried connecting to FTPS using PHP and certificate as auth which didn't work either. As nothing works as it is supposed to, I wonder if the FTPS server is really configured correctly after all.
The people who run the server told me that everything is fine and their CLI CURL-call works fine for them, so they have no need to further investigate issues.
As a result of this I set up a sandbox account on a server which has shell_exec() enabled. There is now a script running which gets a file listing via CURL and then downloads the files via CURL with the commands provided by the server provider. That server can be accessed through normal SFTP and therefore acts as a "Proxy FTP" which regularly mirrors the remote FTPS server file structure.
Although I find this "solution" quite "hacky" it seems to run robust, stable and fast for the moment. We will therefore be able to have the operation running this way in this year (it only runs around three months before christmas) and will have a look into it in the new year and develop a more stable solution.
Maybe the server guys are also less stressed then and willing to help... ;-)
Add the following call to ftp_set_option() in a line before the call to ftp_pasv
ftp_set_option($ftp, FTP_USEPASVADDRESS, false);
ftp_pasv($ftp, true);

PHP mysql persistent connection not reused ( opens more than one connection per fpm-worker )

I'm facing a really weird behaviour while testing persistent connections from php to mysql. I have a small script that looks like this:
<?php
$db = new mysqli('p:db-host','user','pass','schema');
$res = $db->query('select * from users limit 1');
print_r($res->fetch_assoc());
My setup is :
OS: CentOS 7.3
PHP/7.1.18
php-fpm
nginx/1.10.2
MySQL-5.6.30
I tried to do some requests with ab:
$ ab -c 100 -n 500 http://mysite/my_test_script.php
PHP-FPM was configured to have 150 workers ready, and i saw what i was expecting, 150 established connections to mysql, which stayed open after the ab finished. I launched ab once again, and the behaviour was still the same, 150 connections, no new connections where opened. All fine. Then i created a script which did the the same exact requests, same IP, same HTTP headers, but used curl to make the request, and BOOM i had 300 connections on mysql instead of 150. I launched the script again, i got still 300 connections. Subsequent runs of the same script didn't increase the number of connections. Did anyone ever faced anything like this? Does anyone know what could make php open more connections than needed? Am I missing something obvious?
If it's not clear what i'm asking, please comment below and i will try to better my explain problem.
P.S. I tried this with PDO too, same behaviour.
EDIT: My tests where not accurate
After further testing i noticed that my first tests where not accurate. I was in a multi-tenant environment and different connections ( different schema ) where initialized when i launched ab. In my case the php documentation was a bit missleading, it says:
PHP checks if there's already an identical persistent connection (that remained open from earlier) - and if it exists, it uses it. If it does not exist, it creates the link. An 'identical' connection is a connection that was opened to the same host, with the same username and the same password (where applicable).
http://php.net/manual/en/features.persistent-connections.php
Maybe its i obvious to everyone, I don't know, it was not for me. Passing the 4th parameter to mysqli made php consider connections not identical. Once i changed my code to something like this:
<?php
$db = new mysqli('p:db-host','user','pass');
$db->select_db('schema');
$res = $db->query('select * from users limit 1');
print_r($res->fetch_assoc());
The application started to behave as i expected, one connection per worker.

PHP move_uploaded_file over specific ports

Ok, so I've created a project where a client can drag and drop files onto our server and all works great! Now I've been asked to have the files that are being uploaded/transferred by our clients over a specific port range (let's say between 10000 and 11000 for argument sake). I do not know how to accomplish this. My current uploading function looks something like this:
File's name: test/upload.php
$dir = "path/to/directory/";
$tempFile = $_FILE['file']['tmp_name'];
$tagetFile = $dir.$_FILE['file']['name'];
move_uploaded_file($tempFile, $targetFile);
Where $_FILE is a file being uploaded.
Please disregard any syntax or spelling error in the code above, it works perfect at the moment. I have removed a lot of code to give a simplistic idea of what my code is currently doing.
If any configuration changes to PHP are to be made, they need to target this directory specifically as the rest of our website needs to stay on the current port. I am not exactly sure where to begin with specifying the ports to be used for file transfers. The file transfers are purely client to server and will never be vice-versa. We do have an FTP server setup however if possible, we'd like to remain off of it. I am not sure if what I am asking is possible otherwise.
I am using the Dropzone.js plugin (from here: http://www.dropzonejs.com/), however all the PHP code is mine.
I am not sure if something like the code below (from here) is the way to go, I've never used the fsockopen function before.
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
$responding = 1;
if (!$fp) { $responding = 0; }
$tend = microtime(true);
fclose($fp);
All answers are welcome. Thank you.
Since you want the client to upload the file on a different port, you will need to stand up a web server on that port. You could tell your current server to listen on that port, but that would do nothing to reduce load on your main website, so a separate machine is necessary. The new machine will have to be set up to listen to only the file upload port, but will need to contain your server (Apache, etc), PHP, and have access to the network storage location for your files.
If you have one, you may need to configure your firewall so traffic comes in to the correct machine depending on which port it is sent to.
The actual PHP code you use will not really be any different from what you have working now. Your JS code will need to be updated to it posts to the server using port 10000 or whatever you choose.
Here's a simple diagram that may help.

How to connect to ESL from a remote server?

I would like to make a web interface in PHP to see the FreeSWITCH activities (calls, etc), possibly hosted on a different server than the one where FS is running.
I've seen the server status on the FS server using command line (php single_command.php status), but now I would like to see this status from another server.
When I try to copy ESL.php file to this remote server and try to check the status, I get this error message:
Fatal error: Call to undefined function new_ESLconnection() in
/var/www/freeswitch/ESL.php on line 127
This is my index.php file:
<?php
ini_set('display_errors', 1);
$password = "ClueCon";
$port = "8021";
$host = "192.168.2.12";
require_once('ESL.php');
set_time_limit(0); // Remove the PHP time limit of 30 seconds for completion due to loop watching events
// Connect to FreeSWITCH
$sock = new ESLconnection($host, $port, $password);
// We want all Events (probably will want to change this depending on your needs)
$sock->sendRecv("status");
// Grab Events until process is killed
while($sock->connected()){
$event = $sock->recvEvent();
print_r($event->serialize());
}
?>
I undestand that the webserver doesn't have FreeSWITCH installed, so the error message is obvious, but i don't see how to access to this information from this webserver.
Thank you for your help.
Depending upon your need you can use either Inbound or Outbound socket. I do not know much about PHP and FS Event Socket but yeah tried enough with python. I highly recommended to go through thislink.
So if you just want to do small task like initiating a call, bridging any two given number etc i think you should go with Inbound socket(making cli command from your web server to freeswitch server) or mod_xml_rpc.
And if you want to have full control of everything that happens on FS server like showing live call status and modifying their states or say a complete interactive telephony dashboard then you should go with Outbound socket.(Your FS server will send all events to your web server.)
However in your case problem is I think you did not properly build the php ESL module.
this link might help you installing ESL
Rather than using ESL, you might want to consider using the XMLRPC. The connection is very straight forward:
https://wiki.freeswitch.org/wiki/Freeswitch_XML-RPC
The credentials for the XMLRPC are in your autoloads_configs/xml_rpc.conf.xml

PHP fopen function timed out?

any idea why fopen would timeout for a file if it is on my server and I know the url is correct?
update: sorry, i should have mentioned this is in php.
the code is:
fopen($url, 'r');
It works if i put in a relative path for the file, but not if $url is a url in my server (but it works for google.com). Thanks for the help.
Alaitnik's answer was right. The problem only appears when i access my own server files through the ethernet interface. How can I fix this? I need to be able to access the file from the ethernet interface because the url loads dynamically (it's generated from a wordpress cms, so the url doesn't technically exist as a file on my server)
you can use
ini_set('default_socket_timeout',2);
before opening the fopen $url . This actually set the default socket connection timout without responding.
Stream_set_timeout sets time out on the stream that is established via fopn or socket opening functions.
Try this may be helpful for you.
It appears that you're trying to download a file from your own server using the HTTP protocol from a program running on that same server?
If so, the timeout problem is likely to be web server or network configuration related. Timeouts normally only happen because either:
the server really is taking a long time to send back the answer, or
the TCP connection is being blocked
For example, it may be that your local firewall rules only permit access to www.example.com if those queries come from the ethernet interface, but a locally made connection would try to go via the loopback interface.
maybe your "allow_url_fopen" is set to "Off"
check your php.ini file or phpinfo()
If you are trying to get the HTML of a URL, I suggest using curl instead of fopen.
fopen is best used with local files, coz it does not "know" how to deal with the idiosyncrasies of a network resource.
Check the comments on the documentation of fopen. There's a whole lot of gold in there.
Took me ages to solve this, but here I found it, thanks to Alnitak. Opening the file with localhost in the URL instead of the hostname was what did the trick for me.

Categories