FSockOpen Tutorial

Posted by Pete | Posted in PHP Tutorials | Posted on 18-07-2009-05-2008

4

Fsockopen is used to open a socket connection via PHP. In this tutorial we will show you how to test if a port is in use. This can be used as a status script for services.

FSockOpen

fsockopen  ( string $hostname  [, int $port= -1  [, int &$errno  [, string &$errstr  [, float $timeout= ini_get("default_socket_timeout")  ]]]] )

Common Service Ports
HTTP: 80
MYSQL: 3306
FTP: 21
SSH: 22
DNS: 53
POP: 110
SMTP: 25
Please note that these are the common service ports. Although I have listed some common ports you can use this script with any port. If you or your host has configured the services these ports may be different also MYSQL will show as being offline unless the script is running on the same server as the mysql server or mysql has been configured to allow external access.

Tutorial
So what we will do in this tutorial is test if HTTP is running.

The code above is how you would check if http is running on example.com. There are 2 unused items in the tutorial $errono and $errstr. These are used to discover what the error was. Below is an example on how you could use these.

Thank you for reading. If you need any help or like this tutorial please leave a comment.

Comments posted (4)

[...] Here is the original post: FSockOpen Tutorial [...]

i have to warn you, this is the first time i leave a comment on a post about programming asking for help with something but, i am trying to connect to a linux/unix/aix console via ssh (actually i am ussing putty) to connect to it. the idea is to send commands and retrieve the answer of it to do a monitoring console. i was not abble to connect via the ssh extension of php and someone recomend me to use this one, how mai i send commands via fsockopen? i ll be very thankfull for your help, best regards

Its not possible to use fsockopen to send commands. Fsockopen just reads/pings etc. Sorry i cant help.

After some reading up and browsing on PHP.net ive come across http://uk3.php.net/manual/en/ref.ssh2.php which I believe is what your looking for.

Write a comment

You must be logged in to post a comment.

Tutorials Written By Peter Kelly