0

Good Evening,

I've been tasked with launching a URI like file:// however if it doesn't exist or fails to run then the page needs to display a message. I have done a fair amount of research and can't find anything about how to do this. Does anyone know how to catch errors from the header function?

I have tried doing:

try {
  header("Location: URLPATH");
} catch(Exception $e) {
  somecode
}

this doesn't doing anything.

I have also tried:

header("Location: URLPATH");
if (!headers_sent()) {
  echo('Error');
}

this will only work if the header isn't sent, say if I mistype or something like that.

I have also attempted to do the same in javascript however this also doesn't work.

I'm stuck, help please?

Thank you kindly.

Fraser
  • 1
  • 1
    You cannot do what you want. It is just a header being set. The actual redirect is up to the client. The serverside has no idea and doesn't care whether it exists / works. – PeeHaa Dec 09 '14 at 18:59
  • You could first check if the url exists on the server side with curl or file_get_contents and the redirecting to the url. see http://php.net/manual/de/function.file-get-contents.php and http://php.net/manual/de/book.curl.php – WeSee Dec 09 '14 at 19:26
  • Thank you for the comments. This is what I was thinking. – Fraser Dec 10 '14 at 10:25
  • I have just found a solution (it's not the best!) `function sc() {` `window.location.href = "URLPATH";` `window.addEventListener("blur", mf);` `var apc = setInterval(function(){` `window.removeEventListener("blur", mf);` `alert('Application not launched');` `clearInterval(apc);` `}, 1000);` `}` `function mf() {` `window.removeEventListener("blur", mf);` `clearInterval(apc);` `}` – Fraser Dec 10 '14 at 10:35

0 Answers0