Hongmu Notes
Home Language Notes PHP determines whether a web link exists
Language Notes PHP

PHP determines whether a web link exists

PHP determines whether a web link exists

To check whether a link exists, you can use the `curl` function in PHP. `Curl` is a powerful PHP extension that allows you to send HTTP requests and retrieve remote data.

Here is a simple example code for checking whether a link exists:

 隐藏内容:登录后可查看
$url = "http://www.example.com/";  
$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 == 200) {  
    echo "Link exists.";  
  } else {  
    echo "Link does not exist.";  
  }  
} else {  
  echo "Link does not exist.";  
}  

curl_close($curl);

This method uses `curl` to send a HEAD request, thereby avoiding the need to download the entire page content. If the link exists, a 200 status code will be returned. By checking the returned status code, you can determine whether the link exists.

Please note that this method may be disabled by the target website's security policies, or links may not be checked for other reasons. Therefore, it is advisable to handle any potential errors or exceptions with caution when using this approach.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 189
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

Responsive, minimalist article and blog website template – 0751

Responsive, minimalist article and blog website template – 0751 Practical Collection Yiyou template

An EyouCMS responsive website template designed for minimalist article-based blogs. It features a clean and modern design focused on providing an optimal content reading experience. Ideal for individual bloggers, content creators, or news site administrators looking to build a blog platform to share articles, perspectives, and industry updates. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (...
👁 44
(PC+WAP) Special Materials Website Template Special Building Materials Website Source Code Download 1160

(PC+WAP) Special Materials Website Template Special Building Materials Website Source Code Download 1160 Practical Collection pbootcms Template

A PbootCMS website template for special materials and special building materials, supporting PC and WAP. The design style is professional and technological, suitable for displaying special materials, new building materials products and engineering applications. It helps new material companies demonstrate their strength online and attract construction and industrial customers. Template display Installation instructions Website backend:/admin.php Account: admin Password: admin Unzip password: www.4s5.cn phase...
👁 54
(Adaptive mobile version) Black Landscape Photography Studio website – PBootCMS template; Personal portrait photography website source code download – 0422

(Adaptive mobile version) Black Landscape Photography Studio website – PBootCMS template; Personal portrait photography website source code download – 0422 Practical Collection pbootcms Template

An adaptive mobile-friendly PbootCMS website template designed for black-themed landscape photography studios and personal portrait photography businesses. Featuring a cool, artistic design, this template is ideal for photographers and photography studios to showcase their portfolios and services. It helps photography agencies attract high-end clients online and enhance their brand image. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 39
EMLOG backend editor – Set text size

EMLOG backend editor – Set text size Program Notes emlog

When editing text in EMLOG, the font size displayed is too small, making it very uncomfortable to read; if left unchanged, the text is practically impossible to read! To set the font size to 16px, you can modify the following code: Step 1: Open the `admin/views/article_write.php` file, and in the initialization section of both `Editor` and `Editor_summary`, locate the `path:` line...
👁 201
Responsive humorous joke and short comedy article website template – 0755

Responsive humorous joke and short comedy article website template – 0755 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for websites featuring humorous jokes and short comedic articles. Its relaxed and lively design makes it perfect for curating engaging content, images, and anecdotes. It is an excellent choice for building entertainment-focused websites or attracting younger audiences. Template Demo | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (E...
👁 58
(Adaptive mobile phone version) Instrument electrical website template Universal display website source code download 1161

(Adaptive mobile phone version) Instrument electrical website template Universal display website source code download 1161 Practical Collection pbootcms Template

A PbootCMS website template for instrumentation, electrical and general display, supporting PC and WAP. The design style is professional and practical, suitable for instrument and electrical companies to display product series and brand image. It is an efficient choice for electrical instrument companies to build brand official websites. Template display Installation instructions Website backend: /admin.php Account: admin Password: admin Unzip password: www.4s5.cn Related articles Pbo...
👁 53