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.