Asked  7 Months ago    Answers:  5   Viewed   30 times

I wonder if there is any good PHP script (libraries) to check if link are broken? I have links to documents in a mysql table and could possibly just check if the link leads to a the document, or if I am redirected to anther url. Any idea? I would prefer to do it in PHP.

Might be related to: Check link works and if not visually identify it as broken

 Answers

61

You can check for broken link using this function:

function check_url($url) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    $headers = curl_getinfo($ch);
    curl_close($ch);

    return $headers['http_code'];
}

You need to have CURL installed for this to work. Now you can check for broken links using:

$check_url_status = check_url($url);
if ($check_url_status == '200')
   echo "Link Works";
else
   echo "Broken Link";

Also check this link for HTTP status codes : HTTP Status Codes

I think you can also check for 301 and 302 status codes.

Also another method would be to use get_headers function . But this works only if your PHP version is greater than 5 :

function check_url($url) {
   $headers = @get_headers( $url);
   $headers = (is_array($headers)) ? implode( "n ", $headers) : $headers;

   return (bool)preg_match('#^HTTP/.*s+[(200|301|302)]+s#i', $headers);
}

In this case just check the output :

if (check_url($url))
   echo "Link Works";
else
   echo "Broken Link";

Hope this helps you :).

Wednesday, March 31, 2021
 
BetaRide
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
68

You can use method_exists:

if (method_exists($db_manager, $_POST['operation'])){
  $db_manager->{$_POST['operation']}();
} else {
  echo 'error';
}

Though I strongly advise you don't go about programming this way...

Saturday, May 29, 2021
 
jsuggs
answered 5 Months ago
43
$array = @unserialize($string);
if ($array === false && $string !== 'b:0;') {
    // woops, that didn't appear to be anything serialized
}

The $string !== 'b:0;' checks to see if the serialized string may have been the value false. If this check is important to you you may want to trim the serialized string or otherwise preprocess it to make sure this works.

Saturday, May 29, 2021
 
jsuissa
answered 5 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :