How to check if a webpage exists. jQuery and/or PHP

后端 未结 4 593
长发绾君心
长发绾君心 2021-01-05 07:55

I want to be able to validate a form to check if a website/webpage exists. If it returns a 404 error then that definitely shouldn\'t validate. If there is a redirect...I\'m

4条回答
  •  花落未央
    2021-01-05 08:46

    First you need to check that the page exists via DNS. That's why you say it "just hangs" - it's waiting for the DNS query to time out. It's not actually hung.

    After checking DNS, check that you can connect to the server. This is another long timeout if you're not careful.

    Finally, perform the HTTP HEAD and check the status code. There are many, many, many special cases you have to consider here: what does a "temporary internal server error" mean for the page existing? What about "permanently moved"? Look into HTTP status codes.

提交回复
热议问题