Oktay
Oktay

Reputation: 81

How to compare IP and a URL and see if they go to the same website

How can I see in PHP if the IP and a URL are going to the same website?

I have used Curl to get content of the website, and compared with each other to see if the website is the same. If it isn't the same it would say false. This didn't work!

Upvotes: 4

Views: 195

Answers (2)

Ryan
Ryan

Reputation: 3582

You can use gethostbyname for this.

For example

$ip = "1.2.3.4";
$domain = "www.example.com";

if(gethostbyname($domain) == $ip) { echo 'IP/URL Match.'; }

gethostbyname()

Edit:

Quentin pointed out that this doesn't mean they will go to the correct website which is correct. I misread the start of the post. They will only go to the correct website if the default virtual host for the IP address is pointing to the same document root as the URL.

Edit 2:

Now that I think about it, due to the problem highlighed in first edit, the only real way I can think of to find this is to match the content, similiar to how you've done.

There are still flaws with this however, say if the website outputted $_SERVER['SERVER_NAME'] - the content would differ between the two.

Upvotes: 4

Ruub
Ruub

Reputation: 629

I think what you try is this:

A VPS can have multiple users, user1, user2 and user3. If you fil in the IP from this VPS in your browser, you will get: Apache is function normaly.

But the owner of the VPS can also say that the IP from the vps will redirect you to user1 on the vps. Now you won't get the message: Apache is function normaly, but you will get a website.

You wnat to check of that happens?

Upvotes: 2

Related Questions