View Full Version : webserver connection check

04-06-2007, 10:00 AM
Hi guys,

Is it possible to:
after pressing a button on a website witch is redirecting to a new site,
before redirecting to the new site, first check if the website is available,
if not available >> do not try to redirect to the new site.

Just a method to see if the web server (or website) is available before trying to download it.

if possible >> how?


04-06-2007, 01:40 PM
umm that is basically asking it to connect 2 times?

04-07-2007, 03:20 AM
You could use PHP to do that, but definitely not JavaScript.

04-07-2007, 10:07 AM
Actually, it can be done with Javascript. It shouldn't be, but it can.

04-07-2007, 10:38 AM
first check if the website is available,
How can that be done with JavaScript?

file_exists works with PHP, that's the only plausible way of doing it.

04-07-2007, 11:05 AM
How can that be done with JavaScript?If you have the address of an image on that site:
var im = document.createElement("img");
im.src = "http://www.someothersite.com/images/image.png";
im.onload = function() {
// The server is up.
im.onerror = function() {
// The connection timed out, the server
// isn't up (or there was a problem
// accessing the image requested).

04-07-2007, 11:12 AM
I think pear's Net_Ping package might help you.


04-07-2007, 11:46 AM
Not necessarily... just because the host is up, doesn't mean the webserver is. mburt is right, fsockopen() or file_exists() (the latter requires url_fopen enabled) are the better choices.

04-07-2007, 02:07 PM
(the latter requires url_fopen enabled)
I swear. Every person who's ever been on the PHP forum has asked some sort of question involving this, at least ONCE. :)

Just a note for someone who's about to ask:
It's a command in your php.ini which is either, off or on. If it is on, it allows you to basically "connect" to other sites' data. If it's off, you're royally screwed.

04-07-2007, 02:13 PM
What reasons would there be for turning it off?

04-07-2007, 02:23 PM
You don't turn it off or on. You can't. It's your server configuration.

04-07-2007, 02:42 PM
But the configuration can be changed... correct? doesn't editing php.ini turn the features on/off? Am i missing something?

04-07-2007, 02:50 PM
You can edit it in your php.ini file; however, some webhosts turn this off for security issues (or some other reasons). I think, by default, it is turned on.

04-07-2007, 02:50 PM
With W/LAMP you can change it. That's about it. If you get a webhost off the internet, it's set by your host. You often contact your webhosting service though, and ask them to change it. I read a post a while back where someone did.

04-07-2007, 03:51 PM
Not royally screwed, per se... you just have to use fsockopen() and do things the old-fashioned way rather than relying on URLs :)

04-07-2007, 05:11 PM
Yeah, and all the curl/ob functions.

04-08-2007, 05:52 AM
Each time I try to use fsockopen, my browser crashes. No clue why but I am sure it wasn't supposed to happen. :(

04-08-2007, 11:31 AM
Err? It runs on the server, it shouldn't have any impact on your browser...

04-08-2007, 01:06 PM
Yea, I know that, that's why I wrote:

but I am sure it wasn't supposed to happen

But still, it kinda like freezes my browser.

04-08-2007, 01:12 PM
You're sure the page doesn't just take a long time to load?

04-08-2007, 02:53 PM
Actually, it doesn't load at all.