php - Show message if header location redirect fails -
this question has answer here:
- how can check if url exists via php? 15 answers
i need redirect unknown address. if address not available show message user. how that?
<?php header("location: http://www.example.com/"); exit; ?>
the direct method retrieve page:
if (file_get_contents('http://www.example.com/') !== false) { header("location: http://www.example.com/"); exit; }
http://php.net/manual/en/function.file-get-contents.php
however, tells if available @ page. won't tell if, instance, you got 404 error page instead.
for (and save memory cost of downloading whole page), can get_headers()
url instead:
$url = "http://www.example.com/"; $headers = get_headers($url); if (strpos($headers[0],'200 ok') !== false) { // or header("location: ".$url); exit; }
Comments
Post a Comment