Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

validation - How to PHP server-side check if a url of a web site is valid?

I've searched our SO questions but found solutions are based on calling ping command executed from using system PHP function.

My web host server doesn't allow me to do that. What should I do?

Please help, Nam.

Update

I need to check from the server side.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If by a valid URL you mean one which does not 404, then you could use get_headers() and look for 404 in the first returned array element.

$url = 'http://google.com';
list($status) = get_headers($url);
if (strpos($status, '404') !== FALSE) {
   // URL is 404ing
}

Alternatively, you could look for 200 which would be a normal happy request :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...