Asked  2 Years ago    Answers:  5   Viewed   48 times

Running the following Code


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

    [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


You get

    [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


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'));
$info = curl_getinfo($curl);

Also returns the same result



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 = '';

$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.

Sunday, October 2, 2022

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.

Monday, November 21, 2022

This may look slight more complicated then it seems, but I would work out your issue in the following way.

  1. Create a dev domain on the C:WindowsSystem32driversetchosts file pointing to the IP which is the localhost IP.

    The domain name doesn't really matter just something easy for you to use for that given project for example so the hosts file would look like this:

    # localhost name resolution is handled within DNS itself.
    #       localhost
    #   ::1             localhost   localhost
  2. Now define where the folder for that project will be, I will use c:projectstest for this example.

  3. Now create on your web server a new virtualhost for the domain you have just created, here is a sample of virtualhost that I use:

    <VirtualHost *:80>
            ServerAdmin [email protected]
            DocumentRoot "C:/projects/test"
            ErrorLog "C:/projects/logs/test.dev_error_log"
            CustomLog "C:/projects/logs/test.dev_access_log" common
            RewriteLog "C:/projects/logs/test.dev_rewrite_log"
            RewriteLogLevel 9
            ScriptAlias /cgi-bin/ "C:/projects/test/cgi-bin/"
            <Directory "C:/projects/test">
                    Options -Indexes FollowSymLinks +ExecCGI
                    AllowOverride All
                    Order allow,deny
                    Allow from all
            <Directory "C:/projects/test/cgi-bin">
                    AllowOverride None
                    Options None
                    Order allow,deny
                    Allow from all
  4. Restart your server to activate the changes, you do not need to restart the computer for the hosts file, it should work immediately once you save it.

  5. Now you can use or and you can use the very same .htaccess on both, dev and live sites.

Some other tips:

  1. Always use and define RewriteBase / as it can make a difference specially when you want to use rules without the / at the start, like you have mentioned for RewriteRule ^example/$ example.php [L,NC]

  2. Depending on the rules you want to use, you may need to specify -MultiViews so I often just leave it on by default, like this:

    Options +FollowSymLinks -MultiViews
  3. As you can see on my virtualhost setup, I define the access, error and rewrite logs into the logs folder by default and with the domain name in question so when I need to see what is going on I have an easy access with the clear logs for that domain only and not my whole bunch of projects running on the same virualhost.

I guess that is about it, you don't really need that huge virtualhost that's just how I use it and its not like I need to copy and paste it all the time as I have my own simple script to create new domains and do all the work for me.

Wednesday, December 14, 2022

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.


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

Wednesday, December 14, 2022

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

Tuesday, September 20, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages