LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP - fsocket open to shared ip host not working (https://www.linuxquestions.org/questions/programming-9/php-fsocket-open-to-shared-ip-host-not-working-391316/)

BrianK 12-10-2005 01:00 PM

PHP - fsocket open to shared ip host not working
 
I'm trying to post data from my office to my web server. The web server is a single machine that hosts lots of domains - all using the same IP... i.e., if I ping the server (hero.com) it shows the same ip as if I ping my domain.

So when I assemble my header & use my domain name, php turns the domain name into an ip, making the location of the action script no longer valid.

Here's the code:
Code:

$host = "www.mydomain.com";
$hostport = 80;
$action = "/client/process.php";

$req .= "a bunch of stuff";


$header .= "POST $action HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ($host, $hostport, $errno, $errstr, 30);

if ($fp) {
  fputs ($fp, $header . $req);
} else {
  print"Couldn't connect to $host\n";
}

If I just go directly to the site with my browser, it works, but if I run the above php from a command line at my office, the server returns a 404 because it's trying to find the action based on hero.com.

The access logs look like so:


working:
<my ip> - - [10/Dec/2005:10:52:56 -0800] "GET /client/process.php HTTP/1.1" 200 17 mydomain.com "-" "Links (1.00pre12; Linux 2.4.27-2-686-smp i686; 80x24) (Debian pkg 0.99+1.00pre12-1)"
non working (php script)
<my ip> - - [10/Dec/2005:10:53:20 -0800] "POST /client/process.php HTTP/1.0" 404 282 207.151.19.45 "-" "-"


The later shows the ip of hero.com... If I http://207.151.19.45 , I get the main hero.com page.

What do I need to do to make sure it uses mydomain.com instead of the ip? I've tried using the full webserver path to process.php in the above code, but it does not work.

edit: the above code does not print out any sort of error message, so it makes the connection to hero.com or mydomain.com fine, it just can't post the data because it can't find the action.

Spudley 12-10-2005 05:19 PM

You need to use HTTP/1.1 instead of 1.0.

HTTP 1.0 does not support domain mapping -- ie having multiple domains on a single IP address; this was the key difference in 1.1. There were other differences, of course, but that was the main one you'd notice, and I'd be willing to bet it's what's causing the problem here.

For more info, see this document (the section entitled "Internet address conservation" is the bit that's relevant)

Hope that helps. :)

BrianK 12-11-2005 05:28 PM

Quote:

Originally Posted by Spudley
You need to use HTTP/1.1 instead of 1.0.

HTTP 1.0 does not support domain mapping -- ie having multiple domains on a single IP address; this was the key difference in 1.1. There were other differences, of course, but that was the main one you'd notice, and I'd be willing to bet it's what's causing the problem here.

For more info, see this document (the section entitled "Internet address conservation" is the bit that's relevant)

Hope that helps. :)

Interesting. I didn't know about the difference between 1.0 & 1.1. Thanks. ;)

Unfortunately, it didn't help - the same thing happens using HTTP/1.1 in the header... the access log shows it returned a 404 & appears to still have tried to use the ip rather than the domain name... which actually makes sense because the header doesn't come into play until after I've made the socket connection.

I've worked around it by using a get method rather than post & send all the info through the URL. I'd rather not do it that way, so if any other ideas come up, I'd be happy to hear them.


All times are GMT -5. The time now is 12:32 AM.