Asked  7 Months ago    Answers:  5   Viewed   21 times

Running the following Code

var_dump(get_headers("http://www.domainnnnnnnnnnnnnnnnnnnnnnnnnnnn.com/CraxyFile.jpg"));

Returns HTTP 200 instead of 404 For any domain or URL that does not exist

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Server: nginx/1.1.15
    [2] => Date: Mon, 08 Oct 2012 12:29:13 GMT
    [3] => Content-Type: text/html; charset=utf-8
    [4] => Connection: close
    [5] => Set-Cookie: PHPSESSID=3iucojet7bt2peub72rgo0iu21; path=/; HttpOnly
    [6] => Expires: Thu, 19 Nov 1981 08:52:00 GMT
    [7] => Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    [8] => Pragma: no-cache
    [9] => Set-Cookie: bypassStaticCache=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; httponly
    [10] => Set-Cookie: bypassStaticCache=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; httponly
    [11] => Vary: Accept
)

If you Run

var_dump(get_headers("http://www.domain.com/CraxyFile.jpg"));

You get

Array
(
    [0] => HTTP/1.1 404 Not Found
    [1] => Date: Mon, 08 Oct 2012 12:32:18 GMT
    [2] => Content-Type: text/html
    [3] => Content-Length: 8727
    [4] => Connection: close
    [5] => Server: Apache
    [6] => Vary: Accept-Encoding
)

They are so many instances where get_headers has been proven to be a solution to validate existing URL

  • What is the best way to check if a URL exists in PHP?
  • How can I check if a URL exists via PHP?

Is This is a Bug or get_headers is not a reliable way for validating URL

See Live Demo

UPDATE 1

Got to find out that CURL also has the same issue

$curl = curl_init();
curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => true,CURLOPT_URL => 'idontexist.tld'));
curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
var_dump($info);

Also returns the same result

 Answers

86

The problem is nothing to do with the length of the domain name, it is simply whether the domain exists.

You are using a DNS service that resolves non-existent domains to a server that gives you a "friendly" error page, which it returns with a 200 response code. This means it is also not a problem with get_headers() specifically, it is any procedure with an underlying reliance on sensible DNS lookups.

A way to handle this without hardcoding a work around for every environment you work in might look something like this:

// A domain that definitely does not exist. The easiest way to guarantee that
// this continues to work is to use an illegal top-level domain (TLD) suffix
$testDomain = 'idontexist.tld';

// If this resolves to an IP, we know that we are behind a service such as this
// We can simply compare the actual domain we test with the result of this
$badIP = gethostbyname($testDomain);

// Then when you want to get_headers()
$url = 'http://www.domainnnnnnnnnnnnnnnnnnnnnnnnnnnn.com/CraxyFile.jpg';

$host = parse_url($url, PHP_URL_HOST);
if (gethostbyname($host) === $badIP) {
  // The domain does not exist - probably handle this as if it were a 404
} else {
  // do the actual get_headers() stuff here
}

You may want to somehow cache the return value of the first call to gethostbyname(), since you know you are looking up a name that does not exist, and this can often take a few seconds.

Wednesday, March 31, 2021
 
Bere
answered 7 Months ago
48

Edit: The bug is fixed now, if you're still getting the same Exception, please make sure you're updating your Adapter data source only from the main thread and calling appropriate adapter notify method after it.

Old answer: It seems to be a bug in RecyclerView, it's reported here and here. Hopefully it will be fixed in the next release.

Tuesday, June 22, 2021
 
Markol
answered 4 Months ago
87

We are hoping to have Captions support on Data API v3 soon. You won't need to scrape the website.

Update: This has been implemented now. The docs can be found here.

Monday, August 9, 2021
 
DMTintner
answered 3 Months ago
92

Validation is part of the domain logic. Controller should have nothing to do with this. It only has to pass the incoming request values to the proper parts of model layer.

The validation itself should happen in domain objects within the model layer. Also, in some forms you have to worry about data integrity (i.e. unique usernames in registration form). In that case the data integrity checks actually should be handled by data mappers by, essentially, passing data to SQL database, which performs the check and, if there is a violation, it triggers an exception on DB abstraction.

Update

Since your problems is dealing with authentication/authorization, you might find this post relevant.

Thursday, August 12, 2021
 
AlterPHP
answered 3 Months ago
89

In your example you are never incrementing the variable $j.

Try a foreach instead:

foreach($j=2; $j<=10; $j++) {
  //do your stuff
}

Or change it up to be a do-while http://php.net/manual/en/control-structures.do.while.php

Wednesday, August 18, 2021
 
Aran Mulholland
answered 2 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 :