Space does not support fsockopen function solution

Source: Internet
Author: User
For server security considerations, many host vendors disabled php's fsockopen function. They blog yesterday and use cos-html-cache to generate static files. Nima prompts: Warning: fsockopen () hasbeendisabledforsecurityreasonsinD: cos-html-cachecos-html-cachephpon

For server security considerations, many host vendors disabled php's fsockopen function. 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. for details, replace the fsockopen string in the search program with pfsockopen. for example:

Before modification: $ 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: $ 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:

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

Specific operations:

1. first find the code segment using the fsockopen function, add the code above to its upper end, 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, this B _fsockopen function does not return the file pointer. you need to modify the code segment: replace fread with socket_read, and replace fwrite with socket_write, replace fclose with socket_close.

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:

  1. $ Str = file ("http://www.phpfensi.com ");
  2. $ Count = count ($ str );
  3. For ($ I = 0; $ I <$ count; $ I ++ ){
  4. $ File. = $ str [$ I];
  5. }
  6. Echo $ file;
  7. ?>

Method 2:

  1. $ Str = file_get_contents ("http://www.phpfensi.com ");
  2. Echo $ str;
  3. ?>

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.