PHP simple to detect whether the Web site can be opened in a normal way _php tips

Source: Internet
Author: User
Tags php programming php regular expression

This article is an example of how the PHP simple detection URL can be opened in a normal way. Share to everyone for your reference, specific as follows:

This is a check whether the Web site can normally open the PHP code, the following code to detect whether a Web site can be normal access, if normal will return the HTTP status code 200, if the other is not normal; This code we can use a lot of places, such as the caching of the links of the ICO icon can be used, The cache first detects whether the Web site is normal, if you normally cache the ICO icon, otherwise call a default icon file.

The code is as follows:

<?php
*
 * Created on 2016-9-4
 *
 /function Httpcode ($url) {
  $ch = Curl_init ();
  $timeout = 3;
  curl_setopt ($ch, curlopt_followlocation,1);
  curl_setopt ($ch, curlopt_returntransfer,1);
  curl_setopt ($ch, Curlopt_header, 1);
  curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
  curl_setopt ($ch, Curlopt_url, $url);
  Curl_exec ($ch);
  return $httpcode = Curl_getinfo ($ch, curlinfo_http_code);
  Curl_close ($ch);
echo "To judge the links of the cloud-dwelling communities:". Httpcode (' http://www.jb51.net ');
? >
<br/>
If display 200 is normal, if other values indicate abnormal, and 3 after $timeout is the set timeout number of seconds.

The results of the operation are shown below:

More about PHP Interested readers can view the site topics: "Php Curl Usage Summary", "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.