简体   繁体   中英

Validate link href attribute

I need to periodically loop through links in my PHP database to check whether the link leads to valid page. If the link has expired or is invalid, I don't want to output it. How can I check that the href value leads to a valid page efficiently?

Thanks for any *pointers.

Look into curl. It allows you to pull a site in php http://www.php.net/manual/en/function.curl-exec.php Then just check for either a status code on the response or something like a title tag.

You can also use multiple CUrl request each time to check all list more faster. Check here

I'm kind of a noob myself, but I would suggest using cURL. A quick Google search on using revealed the following code (which I haven't tested):

<?php

$statusCode = validate($_REQUEST['url']);
if ($statusCode==’200′)
  echo ‘Voila! URL ‘.$_REQUEST['url'].
  ’ exists, returned code is :’.$statusCode;
else
  echo ‘Opps! URL ‘.$_REQUEST['url'].
  ’ does NOT exist, returned code is :’.$statusCode;

function validateurl($url)
{
  // Initialize the handle
  $ch = curl_init();
  // Set the URL to be executed
  curl_setopt($ch, CURLOPT_URL, $url);
  // Set the curl option to include the header in the output
  curl_setopt($ch, CURLOPT_HEADER, true);
  // Set the curl option NOT to output the body content
  curl_setopt($ch, CURLOPT_NOBODY, true);
  /* Set to TRUE to return the transfer
  as a string of the return value of curl_exec(),
  instead of outputting it out directly */
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  // Execute it
  $data = curl_exec($ch);
  // Finally close the handle
  curl_close($ch);
  /* In this case, we’re interested in
  only the HTTP status code returned, therefore we
  use preg_match to extract it, so in the second element
  of the returned array is the status code */
  preg_match(“/HTTP\/1\.[1|0]\s(\d{3})/”,$data,$matches);
  return $matches[1];
}
?> 

Source: http://www.ajaxapp.com/2009/03/23/to-validate-if-an-url-exists-use-php-curl/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM