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
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:
or
After modification:
or
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:
}
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.
Just tell you the address
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.