This article describes how to disable the fsockopen and pfsockopen functions of PHP by the host service provider. One is to use the stream_socket_client function, and the other is to write a UDF similar to the fsockopen function.
This article describes how to disable the fsockopen and pfsockopen functions of PHP by the host service provider. One is to use the stream_socket_client function, and the other is to write a UDF similar to the fsockopen function.
Maybe the fsockopen and pfsockopen functions do have security risks, but we have no research. This is what IDC vendors say, no matter what the reason is, anyway, they disabled these two functions. How can they be solved? Below is a small compilation method, hoping to have some reference for those who are using it.
The solution is as follows:
I. Use stream_socket_client () to replace
If both fsockopen and pfsockopen are disabled on the server, use other functions, such as stream_socket_client (). Note: The parameters of stream_socket_client () and fsockopen () are different.
Specific operations:
Search for the character string fsockopen in the Program (replace it with stream_socket_client (, and then delete the port parameter "80" in the original fsockopen function and add it to $ host.
Example:
Before modification:
The Code is as follows:
$ Fp = fsockopen ($ host, 80, $ errno, $ errstr, 30 );
Or
The Code is as follows:
$ Fp = fsockopen ($ host, $ port, $ errno, $ errstr, $ connection_timeout );
After modification:
The Code is as follows:
$ Fp = stream_socket_client ("tcp: //". $ host. "80", $ errno, $ errstr, 30 );
Or
The Code is as follows:
$ Fp = stream_socket_client ("tcp: //". $ host. ":". $ port, $ errno, $ errstr, $ connection_timeout );
Ii. Rewrite Method
If PHP version is earlier than 5.0, fsockopen is disabled, and stream_socket_client () is not available, what should I do? Write a function to implement the fsockopen function. Refer to the 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 code above to its top, and search for the string fsockopen in the code segment (replace it with B _fsockopen (.
2. because the fsockopen function returns the file pointer, it can be operated by the file function. However, this B _fsockopen function does not return the file pointer. You need to modify the code segment: Use socket_read (replace fread (, use socket_write (replace fwrite (and use socket_close (replace fclose (.
In the above method, I used stream_socket_client () to solve the problem. The second method did not try.