19

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.

Alex
  • 1,865

5 Answers5

17

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
  • 1,874
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
  • 209
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
  • 6,710
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
  • 584
  • 4
  • 11
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.

Alex
  • 1,865
Journeyman Geek
  • 133,878