Heim  >  Artikel  >  Backend-Entwicklung  >  浅析虚拟主机服务器php fsockopen函数被禁用的解决办法_PHP教程

浅析虚拟主机服务器php fsockopen函数被禁用的解决办法_PHP教程

WBOY
WBOYOriginal
2016-07-21 14:59:291002Durchsuche

一、如何禁用fsockopen()
下面是两种常用的禁用fsockopen的方法。
1、修改php.ini,将 disable_functions = 后加入 fsockopen
2、修改php.ini,将 allow_url_fopen = On 改为 allow_url_fopen = Off

二、如何解决fsockopen函数被禁用
1、如果服务器没有同时禁用pfsockopen,那么直接将fsockopen函数替换为pfsockopen。
具体操作:搜索程序中的字符串 fsockopen( 替换为 pfsockopen( 。示例如下
修改前:
$fp = fsockopen($host, 80, $errno, $errstr, 30);
修改后:
$fp = pfsockopen($host, 80, $errno, $errstr, 30);
2、如果服务器同时禁用了pfsockopen,那么用其他函数代替,如stream_socket_client()。注意:stream_socket_client()和fsockopen()的参数不同。
具体操作:搜索程序中的字符串 fsockopen( 替换为 stream_socket_client( ,然后,将原fsockopen函数中的端口参数“80”删掉,并加到$host。示例如下
修改前:
$fp = fsockopen($host, 80, $errno, $errstr, 30);
修改后
$fp = stream_socket_client($host."80", $errno, $errstr, 30);
3、如果PHP版本低于5.0,fsockopen被禁用,又没有stream_socket_client()怎么办呢?自己写一个函数实现fsockopen的功能,参考代码:

复制代码 代码如下:

function b_fsockopen($host, $port, &$errno, &$errstr, $timeout) {
 $ip = gethostbyname($host);
 $s = socket_create(AF_INET, SOCK_STREAM, 0);
 if (socket_set_nonblock($s)) {
  $r = @socket_connect($s, $ip, $port);
  if ($r || socket_last_error() == EINPROGRESS) {
   $errno = EINPROGRESS;
   return $s;
  }
 }
 $errno = socket_last_error($s);
 $errstr = socket_strerror($errno);
 socket_close($s);
 return false;
}

具体操作:1.首先找到使用fsockopen函数的代码段,将上面代码加至其上端,搜索该代码段中的字符串 fsockopen( 替换为 b_fsockopen(
2.因为fsockopen函数返回文件指针所以可以被文件函数操作,但是这个b_fsockopen函数没能返回文件指针,需要继续修改代码段:用socket_read( 替换掉 fread( ,用socket_write( 替换掉fwrite( ,用socket_close( 替换掉fclose(

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/328145.htmlTechArticle一、如何禁用fsockopen() 下面是两种常用的禁用fsockopen的方法。 1、修改php.ini,将 disable_functions = 后加入 fsockopen 2、修改php.ini,将 allow_url_...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn