Home >php教程 >PHP源码 >设置URL访问指定IP的服务器

设置URL访问指定IP的服务器

PHP中文网
PHP中文网Original
2016-05-25 17:07:471447browse

在代码里面设定url访问对应ip的服务器,适用于lvs等集群服务器的管理

1. url_bind_ip.php

<?php
/*
 * 在代码里面设定url访问对应ip的服务器
 * 适用于lvs等集群服务器的管理
 * 根据需要,可自行添加返回值
 */
 
url_bind_ip(&#39;http://www.xxx.com/index.php?aa=123&#39;,&#39;192.168.10.23&#39;); // 这是示例
 
function url_bind_ip($url, $ip = null) {  
           
    $url = parse_url($url);  
    
    if (!isset($url[&#39;port&#39;])) {  
        if ($url[&#39;scheme&#39;] == &#39;http&#39;){  
            $url[&#39;port&#39;] = 80;   
        } else if ($url[&#39;scheme&#39;] == &#39;https&#39;){  
            $url[&#39;port&#39;] = 443;  
        }  
    }  
       
    $url[&#39;query&#39;] = isset($url[&#39;query&#39;])?$url[&#39;query&#39;]:&#39;&#39;;  
    $url[&#39;protocol&#39;] = $url[&#39;scheme&#39;].&#39;://&#39;;  
    $eol="\r\n";  
    
    $headers = &#39;GET &#39;.$url[&#39;protocol&#39;].$url[&#39;host&#39;].$url[&#39;path&#39;].&#39;?&#39;.$url[&#39;query&#39;].&#39; HTTP/1.1&#39;.$eol.   
               &#39;Host: &#39;.$url[&#39;host&#39;].$eol.   
               &#39;Content-Length: &#39;.strlen($url[&#39;query&#39;]).$eol.  
               $eol.$url[&#39;query&#39;];  
    $fp = fsockopen($ip ? $ip : $url[&#39;host&#39;], $url[&#39;port&#39;], $errno, $errstr, 5);   
     
    if ($fp) {  
        fwrite($fp, $headers);
    } 
     
} 
 
?>

 以上就是设置URL访问指定IP的服务器的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn