How can I verify if a domain redirect to another? – PHP

  php

Q(Question):

How can I verify if a domain redirect to another site? for html xml xhtml etc.

A(Answer):

Hey.

I suppose you could use the get_headers function to check the HTTP headers the domain returns. If the response gives you a 30x status code, it is redirecting you to another location.

The return data from the function would include these two elements, which you could use to determine if, and where to, it is redirecting you.

Array
(
[0] => HTTP/1.1 302 Found
[3] => Location: http://www.example.com
)

A(Answer):

There are a lot of problems with get_headers. I need a little script wich can return four values: 0 – if domain is not online, 1 – if is online , 2 – if redirect, and new domain if redirect. I have 1800 domain to check. I need to be sure for all kind of servers.

A(Answer):

Ok, what you have tried so far?
What problems are you facing?

We won’t write the script for you, but we are more than happy to help you write it.

A(Answer):

Your server is most likely to know the information that you are looking for. PHP is just an intermediary for programmers to their server. If the headers aren’t doing it for you, then look online for the type of server you have and how it handles responses.

A(Answer):

Yes, but all domain are websites on different servers in world.

A(Answer):

So? You’re gonna need to be more clear about what you are trying to accomplish. If you are using your server to send a request, you WILL receive a response. That’s how you know what your user would expect as a response when they are redirected. If you want some way of knowing information AFTER the redirection has occurred, you’d need access to the other server.

A(Answer):

@Adrian4net

That’s not really true. A domain just points to an IP address. It doesn’t necessarily have to have anything to do with a website.

But anyways, to accomplish what you are trying to do (or at least what I think you are trying to do) you just need to follow a fairly simple process.

Consider this pseudo-code (PHPseudo-code, if you will :P):

if( HEAD request was successfull ){
if( Status code is 200 ){
// Server is online and not redirecting you.
}
else if( Status code is 300-399 ){
$location = Location header
// Server is online and redirecting you to $location.
}
else {
// Server is online and not redirecting you,
// but the request was not successful.
}
}
else {
// Server is offline.
}

That could be the skeleton of your script. You just need to fill it in with working code.

You can either do this by using the get_headers function I mentioned earlier, or by sending the request by some other means. (Curl, Sockets, etc…)

LEAVE A COMMENT