How to go to a website on a shared server by its ip address?

18

6

I have a few questions, please help:

Fist, I can access Google search just by typing http://74.125.224.211, because this is the IP address returned by nslookup.

However, I could not do so with the IP addresses returned from www.yahoo.com. How do I go to the Yahoo search page by its IP?

Another example, http://www.allaboutcircuits.com will resolve to 68.233.243.63 by DNS server, but if I go to http://68.233.243.63, I get "Hello world!".

Second, for some reason, there is something wrong with DNS resolvers with my web hosting service (it will not be fixed !!). So commands like

get_file_contents("http://www.allaboutcircuits.com");

will return php_network_getaddresses: getaddrinfo failed: Name or service not known

How do I get around this with IP address,

68.233.243.63

I mean, somehow attach the HTTP hostname parameter to get_file_contents()?

I would like to solve this on my own side (in my code), no troubleshooting/adjustment will be done by the server admin.

user1502776

Posted 2012-09-14T23:25:43.557

Reputation: 493

9

Read about virtual servers and how a single host is able to host many websites.

– wallyk – 2012-09-14T23:30:21.307

I assume there's some reason you can't just use the domain name? – Isaac Rabinovitch – 2012-09-15T03:09:40.363

@Isaac Rabinovitch: user1502776 says "there is something wrong with DNS resolvers with my web hosting service (it will not be fixed !!)" and "I would like to solve this on my own side (in my code), no troubleshooting/adjustment will be done by server admin." – Bavi_H – 2012-09-15T03:51:23.057

3If your server admin is that stupid, you need to go find a different web hosting provider. – Michael Hampton – 2012-09-15T07:30:44.113

Answers

16

In your PHP script, when you access it by IP address, you also need to send a Host: header in your request with the correct domain name.

This question on Stack Overflow explains how to do it: https://stackoverflow.com/questions/356705/how-to-send-a-header-using-a-http-request-through-a-curl-call

Grant

Posted 2012-09-14T23:25:43.557

Reputation: 1 744

10

As wallyk said, it is virtual servers

Take this example for apache

<VirtualHost *:80>
ServerName example2.com
DocumentRoot C:/www/example2.com
</VirtualHost>

<VirtualHost *:80>
ServerName example.com
DocumentRoot C:/www/example.com
</VirtualHost>

In c:\www\example.com make an index file saying "Hello World! example.com"

in c:\www\example2.com make an index file saying "Hellow World! example2.com"

And change your hosts file to

127.0.0.1 example.com
127.0.0.1 example2.com

Then go to http://example.com then go to http://example2.com

So that is how you can have multiple sites per single ip

So basically the answer is, no you won't be able to get that site from the ip.

Kris

Posted 2012-09-14T23:25:43.557

Reputation: 209

I get the point which is IP alone isn't enough. But is there any PHP command that uses IP and hostname which I provide to fetch remote webpages? The command should not query DNS. – user1502776 – 2012-09-15T00:14:28.420

4While this may be technically accurate, it does not address the question. The question is asking how to request a specific site with a known hostname from a server with a known IP, without automatically resolving the IP address through DNS, using a PHP script. It is not asking how to find a hostname corresponding to a given IP. wallyk's comment is more relevant, because his link includes information on the Host header necessary for a server to distinguish between the websites it hosts and serve the correct one. – Bob – 2012-09-15T02:19:04.447

5

You say you want to manually specify both the the host name and the IP address, instead of relying on a DNS server outside of your control. Looking at the comments at the file_get_contents documentation, I see commenters there have solved similar problems by creating a context that contains the needed Host: header. Something like this:

$context = stream_context_create(array('http' => array('header' => 'Host: www.allaboutcircuits.com')));
$data = file_get_contents('http://68.233.243.63/', 0, $context);

Of course, be aware that the disadvantage to manually specifying both the host name and the IP address is that if the IP address changes in the future (for example, if the site moves to a new hosting provider), your code could stop working until you manually update the IP address.

Bavi_H

Posted 2012-09-14T23:25:43.557

Reputation: 6 137

2

The IP address alone isn't enough to uniquely identify which site you want to go to when multiple virtual hosts are running on the same port/server. In that case, you need to specify the Host header in the HTTP request that identifies the server name. You can do this directly via telnet to port 80:

$ telnet 68.233.243.63 80
Trying 68.233.243.63...
Connected to 68.233.243.63.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.allaboutcircuits.com

where you type the telnet command, and the GET and Host lines, and then get the HTML source code returned from the server.

Or you can use curl where you specifically add the header (with the -H flag).

curl 68.233.243.63 -H "Host: www.allaboutcircuits.com"

dr jimbob

Posted 2012-09-14T23:25:43.557

Reputation: 504

1

Second, for some reason, there is something wrong with DNS resolvers with my web hosting service (it will not be fixed !!)

You could use another DNS server on the client side, in case it’s not the web host—8.8.8.8 and 8.8.8.6 are run by Google, and 4.4.4.2 is a common standby. Namebench will let you find the fastest. Alternatively, you could use a proxy service of some sort (I run my own on a VPS, or use Tor, but there are other options).

Since this is PHP, an even easier option would be to run the code on another system. Kludges aren't maintainable.

Journeyman Geek

Posted 2012-09-14T23:25:43.557

Reputation: 119 122

sounds like he's on some shared host, and can't modify DNS, which is why he has the caveat "I would like to solve this on my own side (in my code), no troubleshooting/adjustment will be done by server admin." I don't think he can modify the DNS . . . – ernie – 2012-09-15T00:13:05.660

1He can use an alternate dns server on the client side to test. If his web host isn't setting up vhosts properly, he needs another web host. – Journeyman Geek – 2012-09-15T01:52:43.230