Php verifies whether the URL is a valid function

Source: Internet
Author: User

Example 1

The code is as follows: Copy code

<? Php
Function isValidUrl ($ url ){
 
$ Patern = '/^ http [s]? :\/\/'.
'([0-9] {1, 3} \.) {3} [0-9] {1, 3}'. // URL in IP format-199.194.52.184
'|'. // Allow IP and DOMAIN (DOMAIN name)
'([0-9a-z _!~ * \ '()-] + \.) *'. // Third-level domain verification-www.
'([0-9a-z] [0-9a-z-] {0, 61 })? [0-9a-z] \. '. // second-level domain verification
'[A-z] {2, 6})'. // top-level domain verification. com or. museum
'(: [0-9] {1, 4 })? '. // Port-: 80
'((\/\?) | '. // Verify the part of the file if it contains a file
'(\/[0-9a-zA-Z _!~ \*\'\(\)\.;\? : @ & =\+ \ $, % #-\/] *)?) $ /';
 
If (! Preg_match ($ patern, $ url )){
Die ('The format of the URL you entered is incorrect. Please check it! ');
    }
}

Example 2

The above example only verifies whether the url is normal and does not mean that access is allowed. We can use a method such as the curl function.

The code is as follows: Copy code

$ Url = "http://www.111cn.net ";
$ Curl = curl_init ($ url );
Curl_setopt ($ curl, CURLOPT_NOBODY, true );
$ Result = curl_exec ($ curl );
If ($ result! = False)
{
$ StatusCode = curl_getinfo ($ curl, CURLINFO_HTTP_CODE );
If ($ statusCode = 404)
  {
Echo "URL Not Exists"
  }
Else
  {
Echo "URL Exists ";
  }
}
Else
{
Echo "URL not Exists ";
}

In addition to this function, many php functions such as file, file_get_contents (), and fopen functions can be used for detection.

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.