Space does not support fsockopen function solution

Source: Internet
Author: User
Tags cos ini

For server security considerations, many host vendors disabled the fsockopen function of php. They blog yesterday and use cos-html-cache to generate static files. Nima prompts:

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

Cos-html-cache of other versions is not supported. Then find the following method. =, (The result is invalid because the functions are disabled .)

We have tried to replace functions with other methods.

1. How to disable fsockopen ()

The following are two common methods to disable fsockopen.

1. Modify php. ini and add disable_functions = to fsockopen.
2. Modify php. ini and change allow_url_fopen = On to allow_url_fopen = Off.

II. How to disable the fsockopen function

1. If the server does not disable pfsockopen at the same time, replace the fsockopen function with pfsockopen.
Specific operation: Search for the string fsockopen in the program (replace with pfsockopen (. Example:
Before modification:

The code is as follows: Copy code

$ Fp = fsockopen ($ host, 80, $ errno, $ errstr, 30 );

After modification:
 

$ Fp = pfsockopen ($ host, 80, $ errno, $ errstr, 30 );


2. If pfsockopen is disabled on the server, use other functions, such as stream_socket_client (). Note: the parameters of stream_socket_client () and fsockopen () are different.
Specific operation: 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: Copy code

$ Fp = fsockopen ($ host, 80, $ errno, $ errstr, 30 );

After modification
 

$ Fp = stream_socket_client ($ host. "80", $ errno, $ errstr, 30 );


3. What if fsockopen is disabled and stream_socket_client () is not available if PHP version is earlier than 5.0? Write a function to implement the fsockopen function. Refer to the 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 operations:

1. First find the code segment that uses the fsockopen function, add the code above to its upper end, 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, but 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 (.


Solution 2:

Most fsockopen applications obtain remote page data. For applications that obtain remote pages, PHP has other functions that can be perfectly replaced:
 
Method 1:

The code is as follows: Copy code
<? Php
$ Str = file ("http://www.111cn.net ");
$ Count = count ($ str );
For ($ I = 0; $ I <$ count; $ I ++ ){
$ File. = $ str [$ I];
     }
Echo $ file;
?>

 
 
Method 2:

The code is as follows: Copy code
<? Php
$ Str = file_get_contents ("http://www.111cn.net ");
Echo $ str;
?>

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.