Connecting Tech Pros Worldwide Forums | Help | Site Map

Get http response codes

Bart op de grote markt
Guest
 
Posts: n/a
#1: Oct 30 '06
Hello

I have a website in php in which I have some kind of portal to external
links that come from a database. When a user clicks on a link,
"link.php" is called, does some stuff (e.g. adds 1 to the visit-count
of that certain page) and then forwards the user with a location-header
to the asked page. Now my question: is it possible to get the
response-code of that asked page? E.g. when code 404 is returned, I
can put that in the database and then it's easy for me to know when a
link is broken.

Kind regards,

Bart

Sjoerd
Guest
 
Posts: n/a
#2: Oct 30 '06

re: Get http response codes



Bart schreef op de grote markt:
Quote:
I have a website in php in which I have some kind of portal to external
links that come from a database. When a user clicks on a link,
"link.php" is called, does some stuff (e.g. adds 1 to the visit-count
of that certain page) and then forwards the user with a location-header
to the asked page. Now my question: is it possible to get the
response-code of that asked page? E.g. when code 404 is returned, I
can put that in the database and then it's easy for me to know when a
link is broken.
No. When you send a location header to the client, the client requests
the page it is redirected to. Your PHP page only points to the new
page, but is not involved in the retrieval of it. Therefore, it can not
retrieve the headers or the page itself, and thus not the return code
which is part of the headers.

Michael Fesser
Guest
 
Posts: n/a
#3: Oct 30 '06

re: Get http response codes


..oO(Bart op de grote markt)
Quote:
>I have a website in php in which I have some kind of portal to external
>links that come from a database. When a user clicks on a link,
>"link.php" is called, does some stuff (e.g. adds 1 to the visit-count
>of that certain page) and then forwards the user with a location-header
>to the asked page. Now my question: is it possible to get the
>response-code of that asked page? E.g. when code 404 is returned, I
>can put that in the database and then it's easy for me to know when a
>link is broken.
No, because it's the browser that request the other page, not your
script.

But you could let your script perform such a check from time to time:
Maybe every 10th click on that link (or if the last check was more than
1 month ago) your script itself could send a request to the other site
to check its state. If the check succeeds, store the result in your DB
and send the redirect to the browser.

Micha
Bart op de grote markt
Guest
 
Posts: n/a
#4: Oct 31 '06

re: Get http response codes



Michael Fesser schreef:

Quote:
But you could let your script perform such a check from time to time:
Maybe every 10th click on that link (or if the last check was more than
1 month ago) your script itself could send a request to the other site
to check its state. If the check succeeds, store the result in your DB
and send the redirect to the browser.
>
Micha
Ok, thx, I'm already a lot wiser and maybe this is what I'm going to
do. It's a nice suggestion.

Bart

rh
Guest
 
Posts: n/a
#5: Nov 2 '06

re: Get http response codes



"Bart op de grote markt" <bartwarnez@freegates.bewrote in message
news:1162215727.250479.229640@i42g2000cwa.googlegr oups.com...
Quote:
Hello
>
I have a website in php in which I have some kind of portal to external
links that come from a database. When a user clicks on a link,
"link.php" is called, does some stuff (e.g. adds 1 to the visit-count
of that certain page) and then forwards the user with a location-header
to the asked page. Now my question: is it possible to get the
response-code of that asked page? E.g. when code 404 is returned, I
can put that in the database and then it's easy for me to know when a
link is broken.
>
Kind regards,
>
Bart
>
Yes.

When link.php "does some stuff" have it send a HEAD request to the link
url.

If it comes back as "200 Ok" forward the user to the page, if it comes back
as "404 Not Found" or any other error response show the user an error page
on your site and log the error for that link.

If the response comes back with a 3** redirect, you could parse that url
and check it (possibly set up a loop to check multiple redirects), forward
the user, or just show an error if you don't want the links to redirect
your users.


Rich


Closed Thread