Space does not support Fsockopen function Workaround _php Tutorial

Source: Internet
Author: User
Tags cos
Bought a space today to find the space does not support the Fsockopen function, later on-line to find a few solution space does not support the Fsockopen method, the following for everyone to share, I hope to help you friends.

In order to server security considerations many hosts disabled PHP fsockopen function, yesterday into the blog, using Cos-html-cache to generate static files, the tip:

Warning:fsockopen () have been disabled for security reasons in d:...cos-html-cachecos-html-cache.php on line 35

Change the other version of the Cos-html-cache, or not. Then find the following method. = =, (the result is not, because the function is disabled.) )

Everybody try, rarely have I such a situation, with other alternative functions.

First, how to disable Fsockopen ()

The following are two common ways to disable Fsockopen.

1, modify php.ini, will disable_functions = after adding fsockopen
2. Modify PHP.ini, change allow_url_fopen = on to Allow_url_fopen = Off

Second, how to solve the Fsockopen function is disabled

1. If the server does not disable Pfsockopen at the same time, replace the Fsockopen function with Pfsockopen directly.
What to do: Search for string Fsockopen in the program (Replace with Pfsockopen (. Examples such as the following
Before modification:

The code is as follows Copy Code

$fp = Fsockopen ($host, $errno, $errstr, 30);

After modification:

$fp = Pfsockopen ($host, $errno, $errstr, 30);


2, if the server is also disabled pfsockopen, then use other functions instead, such as stream_socket_client (). Note: The parameters for Stream_socket_client () and Fsockopen () are different.
What to do: Search the program for string Fsockopen (replace with Stream_socket_client (, then, the port parameter "80" in the original Fsockopen function is deleted and added to the $host. Examples such as the following
Before modification:

The code is as follows Copy Code

$fp = Fsockopen ($host, $errno, $errstr, 30);

After modification

$fp = Stream_socket_client ($host. " ", $errno, $errstr, 30);


3, if the PHP version is less than 5.0,fsockopen is disabled, and no stream_socket_client () How to do? Write a function to implement Fsockopen function, reference code:

The code is as follows Copy Code

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 operation:

1. First locate the code snippet that uses the Fsockopen function, add the above code to its upper end, and search for the string fsockopen in the snippet (Replace with B_fsockopen (

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


Solution Two:

Fsockopen Most applications get remote page data, PHP has other functions that can be a perfect substitute for getting remote page applications:

Method 1:

The code is as follows Copy Code
$str = File ("http://www.bKjia.c0m");
$count = count ($STR);
for ($i =0; $i < $count; $i + +) {
$file. = $str [$i];
}
Echo $file;
?>



Method 2:

The code is as follows Copy Code
$str = file_get_contents ("http://www.bKjia.c0m");
Echo $str;
?>

http://www.bkjia.com/PHPjc/633212.html www.bkjia.com true http://www.bkjia.com/PHPjc/633212.html techarticle bought a space today to find the space does not support the Fsockopen function, and later on-line to find a few solution space does not support the Fsockopen method, the following for everyone to share, I hope that friends have ...

  • 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.