Check if web page ok before redirecting?
How do I create a php file "redirect.php" That takes 1 input parameter "URL", have to check the URL Page is not dead before rediecting?
Example
1 开发者_运维问答user clicks on link http://domain.com/Redirect.php?URL=http://example.com/page1.php
2 redirect.php checks if page (http://example.com/page1.php) is ok!
3 if ok redirect user to http://example.com/page1.php
4 if not ok display message "not in service"
You can use CURL to access the web page in code, and see if you get a valid result.
However, I am not seeing any benefit to doing this. Either it's down and they get your 'not in service' message, or it's down and they get the browser message that it's down... In the process, you've doubled the traffic from your site to the target site unnecessarily.
Use curl or something to grab the headers of the redirect and check for the 200 message. That's an "OK" from the webserver.
You should use the CURLOPT_HEADER
option to echo out the options.
See: http://www.php.net/manual/en/function.curl-setopt.php
Try this:
<?php
if (fopen($_GET['url'], "r")) {
header('Location: ' . $url);
}
else {
// Say it is not OK
}
?>
精彩评论