Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2006-11-19
3
201 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

789 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