3

Could somebody help me with the php function fsockopen?

if I call the function like this :

$fp = fsockopen('xywqnda.com', 80, $errno, $errstr, 10);

With an unavailable host domain, it will never return false and I don't understand why!

Zuul
  • 16,217
  • 6
  • 61
  • 88

3 Answers3

1

Ah, you are using UDP. Your original example didn't show this. This changes things. From the PHP manual:

Warning

UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data.

Sarke
  • 2,805
  • 2
  • 18
  • 28
0

try this

ini_set("display_errors","on")

it will show up a warning if the domain is invalid, other than that the function will return TRUE because the file pointer is returned, meaning file was created with success, FALSE will be returned only if it can't create the file.

Gntem
  • 6,949
  • 2
  • 35
  • 48
0
// displays all warnings, notices and errors
ini_set('display_errors', 1); 
error_reporting(E_ALL);

$fp = fsockopen('xywqnda.com', 80, $errno, $errstr, 10);

The connection attempt will timeout after 10 seconds.
You will get a warning because the domain is unavailable.
$errno and $errstr will contain the system error number and error message.
The function will return false, so $fp will be equal to false.

Documentation: fsockopen

Jocelyn
  • 11,209
  • 10
  • 43
  • 60
  • here is my code: error_reporting(E_ALL); ini_set("display_errors","on"); $fp = fsockopen("udp://2.2.2.2", 13, $errno, $errstr); if (!$fp) echo "offline\n"; else echo "online\n"; ?> – Arba Constantin-Andrei Jul 17 '12 at 11:36