Jump to content

Using fsockopen


Clarkeez

Recommended Posts

Hey.

I'm getting really frustrated with this.. I'm new to PHP and want to make a script that checks a port to see if its open or not, something like this..

 

- #check if port is open

- # if it is, show image

- # if it isnt, show a different image

 

Can please someone help me on how I can go about to do this..

Thanks in advance! :)

Link to comment
https://forums.phpfreaks.com/topic/193526-using-fsockopen/
Share on other sites

What I often do when I am unsure how to use something in PHP, is to go to the manual and copy the example code. The online manuals often have user-contributed code which really makes a huge difference to my understanding how it works.

 

Maybe look up this too.

 

if (....some test here...) {

...do something....

} else {

... do something else....

}

Link to comment
https://forums.phpfreaks.com/topic/193526-using-fsockopen/#findComment-1018793
Share on other sites

Ok well I did just that.

 

#Grabbed from the manual

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

 

So. Would this work? (Also, I've added comments, I hope you can look at them and comment on them to help me understand.

<?php
$fp = fsockopen("IPADDRESS", 80, $errno, $errstr, 5);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";  ##So, this is 'echoed' if it DOESN'T return anything? i.e. closed. (this is because of the '!' in "if (!$fp),, which makes it opposite?
} else { ##So, this is 'echoed' if the port IS open?
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);  ##Whats this for?
    }
    fclose($fp);
}
?>

:'(

 

EDIT, So.. would this work for my needs?

<?php
$fp = fsockopen("IP ADDRESS", 80, $errno, $errstr, 5);
if (!$fp) {
    echo "<img src="blah/blah/portCLOSED.gif"";
} else {
    echo "<img src="blah/blah/portOPEN.gif"" {
        echo fgets($fp, 128); ##WTF IS THIS?
    }
    fclose($fp);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/193526-using-fsockopen/#findComment-1018796
Share on other sites

great, you found it. You seem to have the idea. the fgets is to get any reply from the other end after you sent the message. 128 is the number of chars you want as max I think.

 

Best way is to write mickey-mouse test programs and get them working so that you can see how they work. I do this all the time.

Link to comment
https://forums.phpfreaks.com/topic/193526-using-fsockopen/#findComment-1018799
Share on other sites

Ok :) Thanks.

Here is what I've got so far..

<?php
$fp = fsockopen("xx.xxx.xxx.xxx", 80, $errno, $errstr, 5);
if (!$fp) {
    echo '<img src="./blah4.png"';
} else {
    echo '<img src="./splash.png"';
} fclose($fp);
}
?>

But when I run it, just shows blank screen. Tried lots of different ways with "'s and ''s etc etc. and yes, the images are in the same folder as the script.

Any ideas peeps?

Link to comment
https://forums.phpfreaks.com/topic/193526-using-fsockopen/#findComment-1018800
Share on other sites

firstly you need to turn on error display so that you see all the errors.

 

next you need to check your code for syntax errors before you post your questions - we are not here to write the program or to teach you php. You pay money to professionals for that kind of thing or you buy courses or books.

 

You have too many } so there is probably a error message somewhere.

 

It also helps to look at the source after the page has been generated. If you did that, you would see that in the first example, both your img statements are incorrect, which is probably also why it does not work.

Link to comment
https://forums.phpfreaks.com/topic/193526-using-fsockopen/#findComment-1018805
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.