The same domain name corresponds to different IP and access to the specified host file content method

Source: Internet
Author: User

PHP gets a lot of remote host file content methods, such as: File_get_contents,fopen and so on.

<?php  
Echo file_get_contents (' http://demo.fdipzone.com/test.php ');  
? >

But if the same domain name corresponds to a different IP, for example demo.fdipzone.com corresponds to 3 IP192.168.100.101, 192.168.100.102, 192.168.100.103.

You cannot use file_get_contents to get the content of 192.168.100.101 because it is assigned to different hosts based on the load balancing principle, so it is not certain that each time you are accessing the 192.168.100.101 host.

If you want to set the IP host method locally, but if you need to access 192.168.100.101 in the same program, and then access 192.168.100.102, the method for setting IP specified host locally is not possible, because multiple IPs cannot be assigned the same domain name.

Therefore, you need to use the Fsockopen method to access different IP hosts and then access them through the header settings host.

Use Fsockopen to set the Allow_url_fopen in PHP.ini as on.

<?php
/**
* @param String $IP Host IP
* @param String $host Host domain name
* @param int $port Port
* @param String $url the URL to access
* @param int $timeout timeout
* @return String
*/
function Remote_visit ($ip, $host, $port, $url, $timeout) {

$errno = ';
$errstr = ';
$fp = Fsockopen ($ip, $port, $errno, $errstr, $timeout);

if (! $fp) {//Connect fail
return false;
}
$out = "Get ${url} http/1.1\r\n";
$out. = "Host: ${host}\r\n";
$out. = "connection:close\r\n\r\n";
Fputs ($fp, $out);
$response = ';
Read content
while ($row =fread ($fp, 4096)) {
$response. = $row;
}
Fclose ($FP);
$pos = Strpos ($response, "\r\n\r\n");
$response = substr ($response, $pos +4);
return $response;
}
echo remote_visit (' 192.168.100.101 ', ' demo.fdipzone.com ', '/test.php ', 90);
echo remote_visit (' 192.168.100.102 ', ' demo.fdipzone.com ', '/test.php ', 90);
echo remote_visit (' 192.168.100.103 ', ' demo.fdipzone.com ', '/test.php ', 90);
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.