Solved

Check if a remote www-page is online...

Posted on 2006-11-19
3
197 Views
Last Modified: 2007-01-05
Hi,

I have a PHP-page that fetches information from another site, BUT the problem is that the site sometimes is not available or extremely slow. Hence, I would like to check if the site is online, prior to fetching the information from the site. How do I do that in the best way?

I have tried to use both
fsockopen("www.example.com",80);
and
file_get_contents()

Is there someway to set a timeout-limit?

Thanks in advance,
Jan
0
Comment
Question by:madwax
3 Comments
 
LVL 13

Accepted Solution

by:
rhickmott earned 300 total points
ID: 17976721
You shoul be able to do this in the fsock declaration.

fsockopen ( string target [, int port [, int &errno [, string &errstr [, float timeout]]]] )

Basically declare

fsockopen("www.example.com",80,$errno, $errstr,5)

will time out after 5 seconds and you can use the $errno and $errstr values to ask why. You can then do

if (!fsockopen("www.example.com",80,$errno, $errstr,5)) {
      echo("Site is dead");
} else {
     echo("Alive");
}

If you want to set a limit on how long your script spends reading data then you can use

http://us2.php.net/manual/en/function.stream-set-timeout.php

This function shoulw allow you to set a timelimit on streams
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now