Solution for disabling fsockopen and pfsockopen functions of PHP by the host service provider

Source: Internet
Author: User
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.

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.