R
R
Robotex2011-04-17 16:26:06
PHP
Robotex, 2011-04-17 16:26:06

Javascript or PHP: check if a host is reachable?

How to use JavaScript or AJAX + PHP to check if the host exists or not? I need to check if the user has a web server running on the machine or not (localhost is available on a certain port) and if it is running, display one HTML, otherwise another.

Answer the question

In order to leave comments, you need to log in

7 answer(s)
N
niko83, 2011-04-17
@niko83

Maybe try something like this?
var img = new Image();
$(img).load(function () {
alert('Хост доступен');
).error(function () {
alert('не загрузилась');
}).attr('src', 'ссылка на картинку на удалённом хосте');

M
mark_ablov, 2011-04-17
@mark_ablov

fopen() if possible slowly, get_headers()/fsockopen() if necessary as quickly as possible

E
ertaquo, 2011-04-17
@ertaquo

PHP: I don't know how to determine with Javascript. Unless you send an ajax request and see if it fails or not.
$handle = fsockopen('example.com', 80);
if ($handle !== false)
{
print('работает');
fclose($handle);
} else {
print('не работает');
}

Y
yadeveloper, 2011-04-17
@yadeveloper

php.net/manual/en/function.gethostbyname.php

R
Riateche, 2011-04-17
@Riateche

Even if it works, it will fail. In the new opera, for example, the interaction of the localhost and external sites is tightly prohibited. There is even a special error for this, I don’t remember what it’s called.
Look towards java or flash.

K
Kane, 2011-04-17
@Kane

Here is a javascript version. The idea is to use YQL .

R
Robotex, 2011-04-18
@Robotex

Damn, somehow I didn’t think myself that you could just open a socket :) But we don’t know the ip address of a person - he can sit behind a nat.
In general, what is it for? There is a client application launched by the user. And there is a website. The site must check that the application is running and the socket is available and draw a button, otherwise some kind of inscription. By clicking on the button, the site should request data from the client and receive them. How is this implemented?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question