panthro
panthro

Reputation: 24061

Check if an object is valid?

I sign my URLs so they are only valid for a set period of time.

When the URLs are invalid, I get:

This XML file does not appear to have any style information associated with it. The     document tree is shown below.
<Error>
<Code>AccessDenied</Code>
<Message>Access denied</Message>
</Error>

Is there a way to check the object the URL is valid or not, if it's not valid I wish to redirect a user to a generic error page.

Upvotes: 0

Views: 180

Answers (1)

franga2000
franga2000

Reputation: 587

You can user the function string file_get_contents(string $url) to get the output of the URL, then check if the response is what you specified above (Keep in mind that the "This XML file does not have any style..." isn't a part of the respnse).

Here's an example:

<?php
    if (file_get_contents("http://example.com/foo/bar") === $errorResponse) {
        //There was an error
        header("Location: http://example.com/redirect/");
    }
?>

If the error message isn't always the same, you can use an XML parser to check if <Error> exists or just use regex.

Upvotes: 2

Related Questions