Home  >  Article  >  Backend Development  >  Solution to disabling virtual host php fsockopen function

Solution to disabling virtual host php fsockopen function

WBOY
WBOYOriginal
2016-07-25 09:00:09814browse
  1. $fp = fsockopen($host, 80, $errno, $errstr, 30);
Copy the code

After modification:

  1. $fp = pfsockopen($host, 80, $errno, $errstr, 30);
Copy code

2. If the server also disables pfsockopen, use other functions instead, such as stream_socket_client (). Note: The parameters of stream_socket_client() and fsockopen() are different. Specific operation: 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. The example is as follows before fixing:

  1. $fp = fsockopen($host, 80, $errno, $errstr, 30);
Copy code

After modification

  1. $fp = stream_socket_client($host."80", $errno, $errstr, 30);
Copy code

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

  1. function b_fsockopen($host, $port, &$errno, &$errstr, $timeout) {
  2. $ip = gethostbyname($host);
  3. $s = socket_create(AF_INET, SOCK_STREAM, 0);
  4. if (socket_set_nonblock($s)) {
  5. $r = @socket_connect($s, $ip, $port);
  6. if ($r || socket_last_error() == EINPROGRESS) {
  7. $errno = EINPROGRESS;
  8. return $s;
  9. }
  10. }
  11. $errno = socket_last_error($s);
  12. $errstr = socket_strerror($errno);
  13. socket_close($s);
  14. return false;
  15. }
  16. ?>
Copy code

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(, replace fwrite( with socket_write(, use socket_close( Replace fclose(.



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