PHP's Fsockopen, Pfsockopen function is disabled by the host vendor Solution _php Instance

Source: Internet
Author: User

Perhaps Fsockopen, Pfsockopen function does exist security risks, but we have no research, this is the IDC business, no matter what the reason, anyway, they are to disable the two functions, then how to solve it, the following is a small arrangement of methods, Hope that the use of the students have a certain reference.

The workaround is as follows:

First, use Stream_socket_client () instead

The server also disables Fsockopen, Pfsockopen, and replaces it with other functions, such as stream_socket_client (). Note: The parameters for Stream_socket_client () and Fsockopen () are different.

Specific actions:

The string Fsockopen in the search program (replaced by Stream_socket_client (), and then the port parameter "80" in the original Fsockopen function is deleted and added to the $host.

Examples are as follows:

Before modification:

Copy Code code as follows:
$fp = Fsockopen ($host, $errno, $errstr, 30);

Or

Copy Code code as follows:
$fp = Fsockopen ($host, $port, $errno, $errstr, $connection _timeout);

After modification:

Copy Code code as follows:
$fp = Stream_socket_client ("tcp://". $host. " 80″, $errno, $errstr, 30);

Or

Copy Code code as follows:
$fp = Stream_socket_client ("tcp://". $host. ":". $port, $errno, $errstr, $connection _timeout);

Ii. rewriting of the law

What if PHP version below 5.0,fsockopen is disabled, and there is no stream_socket_client ()? Write a function of your own to implement Fsockopen function, reference code:

Copy Code code 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 actions:

1. First find the code snippet that uses the Fsockopen function, add the above code to its top, and search for the string fsockopen in the code snippet (replaced by B_fsockopen.

2. Because the Fsockopen function returns a file pointer so it can be manipulated by a file function, but this b_fsockopen function does not return a file pointer, and you need to continue modifying the code snippet: replace the fread with the Socket_read (Socket_write ( Replace the fwrite (, with Socket_close () Replace the fclose (.

In the above method, I use stream_socket_client () substitution problem to solve, 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.