PHP Check connect to Remote SSL Address - php

Is there any reliable way via PHP to check if a local WebServer (php4) can access a remote SSL address ?
https://xxx.yyy.com
port 443 ?
All I'm interested in ensuring is the connection from the WebServer to the remote address is working and on port 443. I'm not bothered about checking certificates etc..
Thanks
UPDATE:
I've tried this, but is it reliable ?
<?php
function checkConnection()
{
$conn = #fsockopen("www.abc.com", 443, $errno, $errstr, 30);
if ($conn)
{
$status = "Connection is OK";
fclose($conn);
}
else
{
$status = "NO Connection<br/>\n";
$status .= "$errstr ($errno)";
}
return $status;
}
echo checkConnection();
?>

Related

PHP fsocketopen - returns false when port is open

I made this code which is supposed to show whether the program running on the specified port is running.
Now the problem is that it shows "Offline" when my port is clearly open. It however does show online for Google on port 80. What am I doing wrong?
$server = '77.251.97.234';
$port = 43594;
$timeOut = 1;
if ($socket = #fsockopen($server, $port, $errorNo, $errorStr, $timeOut)) {
echo '<span style="color:green;">Online</span>';
} else {
echo '<span style="color:red;">Offline</span>';
}

PHP how to connect with sockets and read the response?

The title explains it all...
How can i connect to an IP using tcp protocol and read/get the response?
I have searched a lot but i didnt find any solution.
$socket = stream_socket_server("tcp://127.0.0.1:22", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
while ($conn = stream_socket_accept($socket)) {
echo fread($conn, 26);
fclose($conn);
}
fclose($socket);
}
is this code ok?
Does the job?
Because it seems it doesn't do the job...
Thanks in advance
As suggested by others; avoid using port 22. I recommend using an obscure (unused) socket port number > 1024 such as 4444. Anything below 1024 normally requires root access.
If you need to test connectivity for 22 have your server script run additional functions.
As for sending a response back to the connected client use stream_socket_recvfrom($socket, $length, 0, $peer) instead of fread()
Then on the client side add a response listener:
client.php
$socket = stream_socket_client('tcp://127.0.0.1:4444');
if ($socket) {
$sent = stream_socket_sendto($socket, 'message');
if ($sent > 0) {
$server_response = fread($socket, 4096);
echo $server_response;
}
} else {
echo 'Unable to connect to server';
}
stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
server.php
$conn = stream_socket_server('tcp://127.0.0.1:4444');
while ($socket = stream_socket_accept($conn)) {
$pkt = stream_socket_recvfrom($socket, 1500, 0, $peer);
if (false === empty($pkt)) {
stream_socket_sendto($socket, 'Received pkt ' . $pkt, 0, $peer);
}
fclose($socket);
usleep(10000); //100ms delay
}
stream_socket_shutdown($conn, \STREAM_SHUT_RDWR);
Run server.php which will listen in an endless loop listening for a non-empty packet
once server.php receives a packet it will respond back to the connected client with the received packet.
Then execute client.php which will send 'message' to server.php
Once sent it will then retrieve and echo the response from server.php which should read 'Received pkt message'
From http://php.net/stream_socket_accept
Accept a connection on a socket previously created by stream_socket_server().
That means it waits that one client wants to connect. (You just bind yourself to the port, but don't connect anything)
And fread is also the wrong function to use with socket_* functions. Correct function would be stream_socket_recvfrom().
But this really isn't what you seem to want. You appearently want to open a connection to some place. So fsockopen() is the right function:
$conn = fsockopen("127.0.0.1", 22, $errno, $errstr);
if (!$conn) {
echo "$errstr ($errno)<br />\n";
} else {
echo fread($conn, 26);
fclose($socket);
}

Query if Shoutcast Server is Offline or Online with PHP

I'm trying to use a script to query if a Shoutcast Server is online or offline. The code below is what I'm using at the moment.
$vt_ip = "ip";
$vt_port = "port";
$output = #fsockopen($vt_ip, $vt_port, $errno, $errstr, 2);
if (!$output) {
echo "<FONT CLASS=f1 COLOR=#DD0000><B>OFFLINE</B></FONT>";
} else {
echo "<FONT CLASS=f1 COLOR=#00DD00><B>ONLINE</B></FONT>";
}
#fclose($output);
But it doesn't update, it is stuck on Offline status.
Any help would be greatly appreciated.
$vt_ip = "ip";
$vt_port = "port";
$conn = fsockopen($vt_ip, $vt_port, $errno, $errstr, 2);
if(!$conn){
echo $errno;
}else{
fwrite($conn, "\n");
$output = fread($conn, 1024);
fclose($conn);
if ($output == "") {
echo "<FONT CLASS=f1 COLOR=#DD0000><B>OFFLINE</B></FONT>";
} else {
echo "<FONT CLASS=f1 COLOR=#00DD00><B>ONLINE</B></FONT>";
}
}
It isn't enough to simply make a TCP connection to a SHOUTcast server to determine if the stream is working. In fact, a SHOUTcast server that is running will always accept yourTCP connection, even if there is no stream for playback.
You must connect, request the stream, and then check the return status code. Once connected, send this data:
GET /; HTTP/1.0
Follow that by a \r\n\r\n. Now, read data back from the stream until you get the \r\n\r\n. Then, you can disconnect. Check the status code from the first response line and see if it's 200. If it is, you've got an active stream.

Checking service fsockopen()

I'm trying to check service on port 9998. To do so I'm using fsockopen() function.
Like this:
$host = "1.1.1.1";
$port = "9998";
$checkconn = fsockopen($host, $port, $errno, $errstr, 1);
if($checkconn >= 1){
echo 'ok';
} else {
echo "$errstr";
}
It always returns "Connection Timed Out - 110", but the port is open and there is service running there.
If I change the port (like 80), it returns successful, but why not on the 9998 port?
A 1 second connect timeout is not very long. Network lag can be enough to trigger that even when the host/port is valid. I would suggest using at least 2.5 - 5 seconds instead.
Also, you should be checking the result of fsockopen() for FALSE instead of < 1 to know when it fails.
Try this:
$host = "1.1.1.1";
$port = "9998";
$checkconn = fsockopen($host, $port, $errno, $errstr, 5);
if(!$checkconn){
echo "($errno) $errstr";
} else {
echo 'ok';
}

socket connection code of php

I am writing a simple php socket code.
Here is my code
<?php
$address="127.0.0.1";
$port=9875;
echo "I am here";
if(false==($socket= socket_create(AF_INET,SOCK_STREAM, SOL_TCP)))
{
echo "could not create socket";
}
socket_bind($socket, $address, $port) or die ("could not bind socket");
socket_listen($socket);
if(($client=socket_accept($socket)))
echo "client is here";
?>
when I run this program my browser only shows waiting for localhost.
Is there any problem in my code?
I am using xammp 1.7.4 .
Another thing I want to know if I want to get a HTTP or FTP request do I have change only the port number?
I verified the code and tested in my system and it works correctly. Showing as "client is here" after running the client.
File Name: server.php
<?php
$address="127.0.0.1";
$port=9875;
echo "I am here";
set_time_limit (0);
if(false==($socket= socket_create(AF_INET,SOCK_STREAM, SOL_TCP)))
{
echo "could not create socket";
}
socket_bind($socket, $address, $port) or die ("could not bind socket");
socket_listen($socket);
if(($client=socket_accept($socket)))
echo "client is here";
socket_close($socket);
?>
First run the server.php file.
File: client.php
<?php
$host="127.0.0.1" ;
$port=9875;
$timeout=30;
$sk=fsockopen($host,$port,$errnum,$errstr,$timeout) ;
if (!is_resource($sk)) {
exit("connection fail: ".$errnum." ".$errstr) ;
} else {
echo "Connected";
}
?>
Now run the client.php
Your output should be like this (as I got in my system)
I am hereclient is here
If not, make sure your firewall is not blocking the request. Temporarily disable antivirus if you have one.
That is the expected behaviour of waiting.
The program you have written is a socket server which is ready to listen to the connection with the specified port, until then it will wait.
You can create a client who connects so that you will see the response "Client is here". The client can be any programming language including PHP.
Below is a sample code in PHP (I didn't verify it).
$fp = stream_socket_client("127.0.0.1:9875", $errno, $errstr);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
// Handle code here for reading/writing
}
You can check this link for sample client code in PHP.
EDIT
$host = "127.0.0.1";
$port = 9875;
$timeout = 30;
$sk = fsockopen($host, $port, $errnum, $errstr, $timeout);
if (!is_resource($sk)) {
exit("connection fail: " . $errnum . " " . $errstr);
} else {
echo "Connected";
}

Categories