tags:

views:

45

answers:

3

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"

+3  A: 

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

Malfist
+3  A: 

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.

Fosco
He may be wanting to do something else besides just display "Not in service". He may want to log it, or produce a search if it's not found or any number of things. But if he is just wanting to display that, I agree, it's pointless.
Malfist
Hi yes I want to log and track the pages if it's alive or dead!Otherwise I wouldn't know my app is redirecting to broken external link!
K001
Also want to block some URL swellIf it's blacklisted then I wouldn't redirect the user at all. (then I show a message that URL has been blocked etc)
K001
A: 

Try this:

<?php
if (fopen($_GET['url'], "r")) {
    header('Location: ' . $url);
}
else {
  // Say it is not OK
}
?>
Kaltas
That's a bad idea, that will require the whole page to be downloaded by the webserver, plus it can still return data to report an error, and this won't tell you. Additionally you have to enable fopen_url's in php.ini opening a significant security vulnerability
Malfist
Humm I didn't know that - if fopen really downloads the page I agree that it shouldn't be used.
Kaltas