Weird behaviour: Apache PHP Windows with FSockopen - php

Consider this simple code:
<?php
error_reporting(E_ALL);
//error_reporting(E_STRICT);
date_default_timezone_set('Asia/Calcutta');
$smtp_server = fsockopen("onelocalserver.com", 25, $errno, $errstr, 120);
if(!$smtp_server)
{
// We have an error, do something
echo " Error found! <br>$errno <br>$errstr";
exit;
}
?>
On Windows XP + php5.3.8 + apache 2.2, i get an error:
Warning: fsockopen(): unable to connect to onelocalserver.com:25 (No connection could be made because the target machine actively refused it. )
However, on the same system where Apache and PHP are installed, if i run php from the command prompt ex: c:\php\php.exe filename.php (where above code is in the filename), then i don't get the error!
The server onelocalserver.com is listening on port 25 : there is an SMTP server running there.
Any ideas? This is really bizzare
Thanks in advance

After several tries with different IP addresses, i realised that the only problem was with fsockopen on port 25. It works well with command line interface but didn't work through the web browser. PHP was earlier installed in windows as CGI. I thought i will see if it still doesn't work when set up as an Apache handler and it works! http://www.php.net/manual/en/install.windows.apache2.php .
Hope this helps someone!

Related

Lost UDP packages with PHP socket_sendto

I have a PHP CMS system, that's sending a UDP package to switch off a device on the local network.
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if (!$sock) {
echo "COULD NOT CREATE SOCKET";
}
$result = socket_sendto($sock, "Ausgang1AUS", 11, 0, "192.168.20.49", 30303);
socket_close($sock);
if ($result) echo "UDP SENT ".$result." BYTES";
This has been working perfectly on our old (dedicated) linux server.
Now since switching to a new (virtual) linux server, these packages never reach their target.
I get no error in PHP (just my "UDP SENT 11 BYTES", but the packages just do not arrive anywhere in the local network.
It DOES work if I send the UDP to the server itself (using it's own IP).
It DOES work if I run this script on another machine in the local network.
It DOES work if I send UDP via netcat, e.g.
echo "Ausgang1AUS" | nc -u 192.168.20.49 30303. So it shouldn't be a general blocking problem.
Firewall and SELinux are both disabled on the machine.
Does anyone have an idea whats happening? Could it be some PHP setting or some user privilege? Is there any tool I could use to find out whats's happening to the UDP package?
I'm pretty lost at the moment...
Edit:
Seems to have nothing to do with php socket_sendto in the end, but connect to the LENGTH of the data sent. So #stark was right, using echo (thus introducing a newline) changed the length from 11 bytes to 12 bytes, and suddenly packages do arrive.
I posted a new question on SuperUser
Is this virtual Linux server running on a Windows VM? There was a recent Windows update that introduced a bug in hypervisor which is causing UDP packets under 12 bytes to fail checksum validation.
There's a Github issues here where other users are reporting the same issue on WSL2.
https://github.com/microsoft/WSL/issues/8610
You can see there's a workaround posted in that thread that might help in your situation, at least until Microsoft manage to fix the issue:
ethtool -K eth0 tx off

Curl Error Hostname Not Found in DNS Cache

I have a website (www.example.com | 123.456.789), in that machine i only have 1 web app running. i am trying to use php file_get_content() to a file which in my own server (located on some file like /var/www/my_site/public_html).
The PHP code i am referring is:
$url = 'http://example.com/282-home_default/short-wallet-tan.jpg';
var_dump($url);
$json = #file_get_contents($url);
var_dump($json);
die();
However it always returns error. When i try to do it manually, using CURL
I can confirm that i can CURL other website such as ~$ curl google.com
Can anyone suggest me what to do to resolve this. Thanks
I am using Ubuntu 14.04 LTS to host my webapp.
Some of the solution regarding this question, points out that i should downgrade my version of curl. Did that but still have the same problem.
curl failed to connect to that host's TCP port number 80. You're either not running the http server on port 80, you've made curl try to connect to the wrong server or there's something else in your network that prevents the connect handshake to succeed.

Apache / PHP Unable to communicate with Tomcat JavaBridge

My project uses PHP JavaBridge, I have installed WAMP, JRE 6.0, and Tomcat 6.0.32
I can now access http://localhost:8080/ successfully but when I run my PHP site it gives me the following error
warning: fsockopen() [function.fsockopen]: unable to connect to localhost:8080 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. )
Can anyone please help me with this issue?
EDIT
Forgot to mention that I am using Windows 7
My comment above worked as a solution and hence this answer, so that in future others get it.
Can you 127.0.0.1:8080 instead of localhost:8080? See what do you get?
fsocketopen takes the hostname and it should be a valid domain, in your case, (in general) it could be a valid domain as long as you have an entry for localhost in your hosts file (C:\Windows\System 32\drivers\etc\hosts). See if you have something similar to that.

fsockopen issue when opening port to sphinx only in php

I have an issue when connecting php and only php to the sphinx daemon. I have tested the sample sphinx php example test.php and narrowed it down to being a connection problem.
I have the follow piece of code which shows the problem,
<?php
$fp = #fsockopen ( '127.0.0.1', '9312', $errno, $errstr,300 );
if(!$fp) {
echo "$errstr ($errno)";
}
?>
Whenever it runs from command line or browser I get the following error,
Connection refused (111)
I have verified the following,
searchd is indeed running and on port 9312
searchd can be connected to using telnet
searchd can be connected to using test.py in the sphinx api
php can connect to port 80
It is running on a Westhost VPS which seems to be the issue since it works fine on my local test machine. I have existing code which was using Python to run searches against the index without any issues so this one has me well and truly stumped.
Some additional info,
PHP 5.2.5
Sphinx 0.9.9-release
Anyone have any ideas how I could diagnose and fix this issue further?
Maybe you could use cmd: netstat -an
To see what ip:port is Sphinx Server running, then use that ip and port in fsocketopen() function

php fsockopen

I have a simple php script on a server that's using fsockopen to connect to a server.
<?php
$fp = fsockopen("smtp.gmail.com", 25, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
echo fgets($fp, 1024);
fclose($fp);
}
?>
The problem is the the script times out and fails to connect. If i change the port from 25 to 80 for example it works without problems on any host. So the problem seems to be only the port 25 no matter what host i use, i tried a lot of them and all work for port 80 and others but for 25 fails.
Connections are not blocked form firewall as if i telnet from shell it successfully connects to any port on any host.
Any idea what could be the problem as it's really weird?
LE: If i run the same php script from the shell, php scriptname.php it works so only when i run it by http it fails. I have apache with SuPHP so the problem is around here somewhere
Interesting...
Some firewalls can block specific program's connections to specific ports.
Please check it again, try to stop firewall completely. Also try to stop any anti-spyware.
Like maxnk mentioned firewalling is the most likely issue, either on the server, or by your ISP. Port 25 is frequently firewalled as a method to prevent spam.
Just as a quick test, since you mentioned gmail, you might want to try connecting to port 587 instead. Gmail listens for smpt on this alternate port in addition to port 25 to help users bypass overly restrictive firewalls.
I've run into some strange issues with PHP's socket handling, too. It ended up being a problem with the system it was running on. Have you tried running your code on a different machine?
I think the connection problem is with your machine. I just copied your code into a script on my machine(linux suse) and ran it with php -f test_script. I got the following message
220 mx.google.com ESMTP j8sm1814228gvb.0
CentOS can have SELinux enabled which can cause connection weirdness. Have you checked your error logs?

Categories