Hosting & Domaining Forum

Hosting Discussion => Hosting Software and Control Panels => Topic started by: sebco on Mar 08, 2023, 03:01 AM

Title: Request for a non-existent domain, knowing the IP
Post by: sebco on Mar 08, 2023, 03:01 AM
What if there's a website hosted on a linked domain, but the domain owner fails to pay and the registrar deletes it? However, by knowing the hosting's IP and adding an entry for yourself in the hosts file, you can still access the site by typing in the domain in the address bar.

Is there a way to send a GET request in PHP to a known IP address, but still have the desired domain transferred (without making global changes to the hosts file) and receive the content of the site associated with that configured domain?

Can it be done by passing it through the host header?
Title: Re: get request for a non-existent domain, knowing the IP
Post by: freeplusac on Mar 08, 2023, 04:05 AM
I successfully connected a domain to a hosting, but it is already fully occupied by another website. I implemented the code provided in the response, and the request was processed correctly.

Based on my understanding, the following code should work:

<?php
$ch 
curl_init('1.2.3.4');
curl_setopt($chCURLOPT_HTTPHEADER, ['Host: example.com']);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
echo 
curl_exec($ch);
Title: Re: get request for a non-existent domain, knowing the IP
Post by: jackgrylls on Mar 08, 2023, 05:20 AM
By adding a domain to your hosts file and associating it with an IP address, you essentially instruct your system to bypass DNS queries and directly send requests to that specific IP. This process is essentially the same as typing in the domain name in a web browser and making a request to the associated IP.

However, it's important to note that this method does not grant access to VirtualHosts on the server. Typically, the server routes requests based on ports and internal IP addresses, which cannot be determined by assigning an external IP address to yourself.

If you require guidance on how to send requests to an IP address using a specific domain name, I recommend exploring examples and solutions provided on platforms like SuperUser.
Title: Re: Request for a non-existent domain, knowing the IP
Post by: SteveD on Aug 24, 2024, 12:59 AM
Yes, it's possible to send a GET request to a specific IP address while specifying a domain name using PHP. This can be useful when you need to access a website associated with a domain that has expired or been deleted, but you still know the IP address where the site is hosted. You can achieve this by using the Host header in your HTTP request.

Here's a detailed guide on how to do this in PHP:

Use the stream_context_create Function: PHP's stream_context_create function allows you to set HTTP headers for your request. You can use this to specify the Host header, which will tell the server which domain you're trying to reach, even if you are connecting to it via its IP address.

Create the Context with the Host Header:

$contextOptions = [
    'http' => [
        'header' => "Host: example.com\r\n",
    ],
];
$context = stream_context_create($contextOptions);

Replace example.com with the domain you want to access. This will tell the server to respond as if the request was made to that domain.

Send the GET Request Using file_get_contents:

$ipAddress = '123.123.123.123';  // Replace with the actual IP address
$url = "http://$ipAddress/somepath";  // Replace with the actual path
$response = file_get_contents($url, false, $context);
This will make a request to the specified IP address but use the Host header to make it appear as though the request is intended for the specified domain.

Handle the Response:

if ($response === FALSE) {
    echo "Failed to retrieve content.";
} else {
    echo $response;
}

This will output the content retrieved from the server. You can further process or display this content as needed.

Key Points to Note:
DNS Resolution: The Host header allows the server to correctly respond to requests for virtual hosts, but DNS resolution still needs to be done to route the request to the correct IP. In this case, since you're using the IP directly, this is not a concern.

Server Configuration: The server must be configured to handle requests for multiple domains based on the Host header. This is typically the case with modern web servers hosting multiple sites on the same IP.
Security and Access: Ensure that you have proper authorization to access and retrieve content from the server.
This approach provides a way to bypass domain resolution issues by leveraging the HTTP Host header to specify the desired domain while sending the request to a specific IP address.
Title: Re: Request for a non-existent domain, knowing the IP
Post by: seennyrob on Jul 05, 2025, 05:25 AM
Use cURL, a real workhorse for HTTP requests, to set the target IP with CURLOPT_URL and override the domain with CURLOPT_HTTPHEADER by passing Host: yourdomain.com.
This tricks the server into thinking the request is for that domain, even if DNS is kaput. Just ensure the hosting server is configured for name-based virtual hosting (most are). Here's a quick snippet: $ch = curl_init('http://<IP>'); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Host: yourdomain.com']);. Fetch the response with curl_exec() and you're golden.

No need to mess with the hosts file or DNS hacks. Keep in mind, if the server's got strict SNI or SSL certs, you might hit a wall with HTTPS-stick to HTTP or handle cert errors with CURLOPT_SSL_VERIFYPEER. This is a solid workaround for accessing a site when the domain's gone AWOL, but don't rely on it for prod environments, it's more of a quick-and-dirty fix.