curl_getinfo returns HTTP 200 status code with non

2019-05-21 02:54发布

问题:

I check wether a site site is up-and-running by checking for a HTTP 200 status code:

<?php
$url = 'www.proxyserver-demo.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (200==$retcode)
    {
    echo "All's well";
    }
else
    {
    echo "not so much";
    }
?>

Every site I try however, even non-existing ones return 200 OK status without fail, which should not be possible in the case of the non-existing ones. (my code always outputs "All's well")

My OS is Centos linux, what could be the matter here?

回答1:

sorry for abusing the answer field for what should probably be a comment but I can't really think of a way to present this info properly otherwise. I honestly don't know yet how to whip them comments into any properly displayed and formatted shape. :-|

Anyway, since your code should be working fine but for you it always retuns "everything OK" while you check for a HTTP response, it could be that your ISP is doing DNS-highjacking, which basically is returning fake ip-address data, redirecting you to their own server, usually for monetary gains under the guise of userfriendlyness. (ISP-page that shows "this page doesn't exist" combined with ads or the offer of services such as domain registration, etc..)

an easy way to test is just to ask for DNS data with an application like dig. A query to a properly functioning DNS server for a non-existing domain should return NXDOMAIN as its status:

so when we check for a totally fake domain notexisting.fake with the following command: dig A notexisting.fake., this is what it generally should give:

harald@Midians_Gate:~$ dig A notexisting.fake.

; <<>> DiG 9.7.3 <<>> A notexisting.fake.
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 28725
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0

;; QUESTION SECTION:
;notexisting.fake.              IN      A

;; AUTHORITY SECTION:
.                       1780    IN      SOA     a.root-servers.net. nstld.verisign-grs.com. 2012061700 1800 900 604800 86400

;; Query time: 24 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Jun 17 18:05:10 2012
;; MSG SIZE  rcvd: 109

as you can see,asking for this nonexisting domain notexisting.fake. returns us

;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 28725

Whereas when we query for google.com, with dig A google.com.

harald@Midians_Gate:~$ dig A google.com.

; <<>> DiG 9.7.3 <<>> A google.com.
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13223
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;google.com.                    IN      A

;; ANSWER SECTION:
google.com.             300     IN      A       74.125.132.138
google.com.             300     IN      A       74.125.132.139
google.com.             300     IN      A       74.125.132.101
google.com.             300     IN      A       74.125.132.113
google.com.             300     IN      A       74.125.132.100
google.com.             300     IN      A       74.125.132.102

;; Query time: 29 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Jun 17 18:05:20 2012
;; MSG SIZE  rcvd: 124

our query returns us the proper NOERROR

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13223
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 0

and returns us the ip address found for the A record

;; ANSWER SECTION:
google.com.             300     IN      A       74.125.132.138
google.com.             300     IN      A       74.125.132.139
google.com.             300     IN      A       74.125.132.101
google.com.             300     IN      A       74.125.132.113
google.com.             300     IN      A       74.125.132.100
google.com.             300     IN      A       74.125.132.102

So if your query for the fake domain returns an ip address then you know the problem is with your DNS and your isp no doubt redirects every non-existing request to a server of their own, messing with your 'is my site up strategy' since you'll get a 200 OK status but it'll effectively be from an imposter.

If this is the case then you can only:

  • change your DNS server: use Google Public DNS (8.8.8.8 and 8.8.4.4) or opendns (208.67.222.222 and 208.67.220.220)
  • plead/complain with your isp for them to change this policy or to supply you with your own non-redirecting DNS server
  • run your own private DNS server for your personal lookups if allowed on your network.


标签: php http curl web