Using remote files in PHP

xiaoxiao2021-03-06  106

First, introduction

As long as the allow_url_fopen option is activated in the php.ini file, you can use the HTTP and FTP URL to replace the file name in most functions that require file names as parameters. At the same time, you can use the URL in include (), include_once (), request (), and a request_once () statement. For more information on the protocol supported by PHP, see Appendix J.

Note: To use the URL in PHP 4.0.3 and its later versions, you need to configure PHP with theNenable-URL-FOPEN-WRAPPER parameter. Remote access from the following functions before the Windows version of the PHP version 4.3: include (), include_once (), request (), request_once (), and the ImageCreateFromxxx function in the XLI, the Image Function library extension library.

For example, you can use the following example to open the file on the remote web server, resolve the output data you need, and then use the data in the database, or simply output it to the style matching of the remaining content of your website. .

Example x. Fopen () example

Example X. Get the title of a remote file

unable to open remote file./n"; exit; } While (! Feof ($ file) {$ line = fgets ($ file, 1024); / * this only works if the title and its tags are on one line * / if (EREGI (" (. * </ title> ", $ line, $ u)) {$ title = $ out [1]; break;}} fclose ($ file);?></p> <p>If you have a link to a user's identity and an FTP server with a user's identity and an FTP server, you can also write to the FTP server. You can only create new files with this method; if you try to overwrite the already existing file, the invoke of the FOPEN () function will fail.</p> <p>To connect the server with a username other than "Anonymous", you need to indicate the username (even password), such as "ftp: // user: password@ftp.example.com/path/to/file". (If you need basic authentication when accessing remote files via HTTP protocol, you can also use the syntax used.)</p> <p>Example X. Data Storage of Remote Services</p> <p><? PHP $ file = fopen ("ftp://ftp.example.com/incoming/outputfile", "w"); if (! $ file) {echo <p> unable to open remote file for Writing./ n "; exit;} / * Write the data here. * / fputs ($ file, $ _server ['http_user_agent']." / n "); fclose ($ file);?> Note: You may be from the above example Release inspiration, use this technology to store remote log files. However, as mentioned above, in the URL that is opened in a FOPEN (), you can only write a new file. If the remote file already has a fopen () function will fail. To perform a similar distributed log operation, you can refer to the Syslog () function.</p> <p>In the following content, I will describe the application of this function with more examples.</p> <p>2. Application of Post & GET in PHP</p> <p>To use the POST & GET of PHP, you can use the fsockopen function:</p> <p>Example 1. Fsockopen () Example</p> <p><? PHP $ fp = fsockopen ("www.example.com", 80, $ errno, $ errstr, 30); if (! $ fp) {echo "$ Errstr ($ Errno) <br /> / n"; } else {$ out = "get / http / 1.1 / r / n"; $ out. = "Host: eXample.com/r/N"; $ out. = "Connection: Close / R / N / R / N "</p> <p>FPUTS ($ fp, $ out); while (! Feof ($ fp)) {echo fgets ($ fp, 128);} fclose ($ fp);}?></p> <p>Example 2. USING UDP Connection</p> <p><? PHP $ fp = fsockopen ("udp: //127.0.0.1", $ errno, $ errstr); if (! $ fp) {echo "error: $ Errno - $ Errstr <br /> / n" Else {FWRITE ($ FP, "/ N"); Echo Fread ($ fp, 26); fclose ($ fp);}?></p> <p>Example 3</p> <p><? PHP</p> <p>/ / Define some connection parameters $ URLS = array ('host' => 'localhost', 'port' => 80, 'Path' => '/ index.php', 'Method' => 'POST', 'Protocol '=>' 1.0 ',);</p> <p>// PST method passed the parameter $ ps = array ('language' => 'php', 'Linux' => 'redhat',);</p> <p>// GET method delivery parameter $ GS = array ('php' => 5, 'redhat' => 9);</p> <p>/ ** Back: String (String) Parameters returned after POST or GET method: $ USLS: String $ PS: Array $ GS: Array Call Method: GetData ($ URLS, $ PS, '') // Using POST Method GETDATA ($ URLS, '', $ GS) // Using GET Method Reference: http://cn.php.net/manual/en/function.fsockOpen.php * / Function GetData ($ URLS, $ PS = '', $ gs = '') {$ host = $ urls ['Host']; $ port = $ urls ['port']; $ path = $ urls ['Path']; $ method = $ urls [' Method ']; $ protocol = $ urls [' protocol ']; $ posts ='; $ gets = '; if (is_ARRAY ($ PS)) {Foreach ($ PS AS $ K => $ V) {$ POSTS. = Urlencode ($ K). "=". Urlencode ($ V). '&';} $ posts = Substr ($ pos, 0, -1); $ LEN = Strlen ($ pos);} if IS_ARRAY ($ GS) ($ GS AS $ K => $ V) {$ gets. = Urlencode ($ K). "=". Urlencode ($ V). '&';} $ gets = SUBSTR $ gets, 0, -1);}</p> <p>$ fp = fsockopen ($ Host, $ Port, $ Errno, $ Errstr, 3); if (! $ fp) {echo "can't connect ... / r / n <br> Error: $ Errstr"; Return FPUTS ($ FP, "$ Method $ PATH? $ Gets http / $ protocol / r / n"); FPUTS ($ FP, "Host: LocalHost / R / N"); if ($ posts! = '' ) {FPUTS ($ FP, "Content-Type: Application / X-WWW-FORM-URLENCODED / R / N"); FPUTS ($ FP, "Content-Length: $ LEN / R / N"); FPUTS ($ FP, "/ r / n"); FPUTS ($ FP, $ Posts);} fputs ($ FP, "Connection: Close / R / N / R / N"); $ s = ''; do {$ Data = FGETS ($ FP, 1024); if ($ DATA == ') {Break;} $ s. = $ data;} while; fclose ($ fp); return $ s;}</p> <p>/ / Here is the string Echo getData ($ URLS, $ PS, ') returned using the POST method; // If you want to use the get method: echo getdata ($ URLS,' ', $ GS );?></p> <p>Third, Unicode Vulnerability Attack</p> <p>Code: <? PHP $ fp = @ FOPEN ($ URL, "R") or Die ("Cannot Open $ URL"); while ($ line = @ fgets ($ fp, 1024)) {$ contents. = $ LINE } echo $ contents; // Display file content fclose ($ fp); // turn off file?></p> <p>use:</p> <p>/ Xxxx.php?url=http://target/script/..</div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-101400.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="101400" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.040</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'lNWI0AE7OYbZgdkQu_2ByPryu_2Bo5U45l714gcTKpwEowivJ6MJW3CaLoDlq_2BEPLR733VH9eO76EXn_2F7vpi'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>