Home  >  Article  >  Backend Development  >  Solution to the problem that PHP's fsockopen and pfsockopen functions are disabled by the host provider, fsockopen function_PHP tutorial

Solution to the problem that PHP's fsockopen and pfsockopen functions are disabled by the host provider, fsockopen function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:18994browse

Solution to the problem that PHP’s fsockopen and pfsockopen functions are disabled by the host provider, fsockopen function

Maybe the fsockopen and pfsockopen functions do have security risks, but we have no way to verify it. This is what IDC said. No matter what the reason is, they have disabled these two functions anyway, so how to solve it? Well, here is the method compiled by the editor. I hope it will be a reference for students who use it.

The solution is as follows:

1. Use stream_socket_client() instead of

If the server disables fsockopen and pfsockopen at the same time, use other functions instead, such as stream_socket_client(). Note: The parameters of stream_socket_client() and fsockopen() are different.

Specific operations:

Search for the string fsockopen( in the program and replace it with stream_socket_client((). Then, delete the port parameter "80" in the original fsockopen function and add it to $host.

An example is as follows:

Before modification:

Copy code The code is as follows:
$fp = fsockopen($host, 80, $errno, $errstr, 30);

or

Copy code The code is as follows:
$fp = fsockopen($host, $port, $errno, $errstr, $connection_timeout);

After modification:

Copy code The code is as follows:
$fp = stream_socket_client(“tcp://”.$host.”80″, $errno , $errstr, 30);

or

Copy code The code is as follows:
$fp = stream_socket_client("tcp://".$host.":".$port , $errno, $errstr, $connection_timeout);

2. Rewriting method

What if the PHP version is lower than 5.0, fsockopen is disabled, and there is no stream_socket_client()? Write a function yourself to implement the function of fsockopen, reference code:

Copy code The code is as follows:

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;

}

Specific operations:

1. First find the code segment that uses the fsockopen function, add the above code to the top of it, and search for the string fsockopen( in the code segment and replace it with b_fsockopen( .

2. Because the fsockopen function returns the file pointer, it can be operated by the file function. However, the b_fsockopen function fails to return the file pointer. You need to continue to modify the code segment: replace fread( with socket_read( ) and fwrite( with socket_write( , Replace fclose( with socket_close(.

Among the above methods, I used stream_socket_client() instead to solve the problem. I did not try the second method.

The Win host has disabled the functions fsockopen and pfsockopen and cannot call the API. How to solve it?

Just tell you the address

How to change the fsockopen pfsockopen function of the wordpress program installed on the virtual host?

Modifying fsockopen pfsockopen requires modifying the PHP configuration file php.ini. This file is a PHP program file and is placed in the PHP directory. It is not a file of wordpress.

Wordpress is a web page program that uses PHP for parsing. The functions it relies on must be supported by PHP. If you want to change the configuration of PHP, please find php.ini in the directory where PHP is located and modify it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840756.htmlTechArticleThe solution to the problem that PHP’s fsockopen and pfsockopen functions are disabled by the host provider. The fsockopen function may indeed exist. There are potential safety hazards, but we have no way to test them...
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