Longhorn PHP 2019 Schedule

湖北福彩22选5走势图:Using remote files

湖北十一选五官网 www.na503.cn As long as allow_url_fopen is enabled in php.ini, you can use HTTP and FTP URLs with most of the functions that take a filename as a parameter. In addition, URLs can be used with the include, include_once, require and require_once statements (since PHP 5.2.0, allow_url_include must be enabled for these). See Supported Protocols and Wrappers for more information about the protocols supported by PHP.

For example, you can use this to open a file on a remote web server, parse the output for the data you want, and then use that data in a database query, or simply to output it in a style matching the rest of your website.

Example #1 Getting the title of a remote page

<?php
$file 
fopen ("//www.example.com/""r");
if (!
$file) {
    echo 
"<p>Unable to open remote file.\n";
    exit;
}
while (!
feof ($file)) {
    
$line fgets ($file1024);
    
/* This only works if the title and its tags are on one line */
    
if (preg_match ("@\<title\>(.*)\</title\>@i"$line$out)) {
        
$title $out[1];
        break;
    }
}
fclose($file);
?>

You can also write to files on an FTP server (provided that you have connected as a user with the correct access rights). You can only create new files using this method; if you try to overwrite a file that already exists, the fopen() call will fail.

To connect as a user other than 'anonymous', you need to specify the username (and possibly password) within the URL, such as 'ftp://user:[email protected]/path/to/file'. (You can use the same sort of syntax to access files via HTTP when they require Basic authentication.)

Example #2 Storing data on a remote server

<?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. */
fwrite ($file$_SERVER['HTTP_USER_AGENT'] . "\n");
fclose ($file);
?>

Note:

You might get the idea from the example above that you can use this technique to write to a remote log file. Unfortunately that would not work because the fopen() call will fail if the remote file already exists. To do distributed logging like that, you should take a look at syslog().

add a note add a note

User Contributed Notes 3 notes

up
6
slva dot web dot sit at gmail dot com
5 years ago
If  allow_url_fopen is disabled in php.ini you can use CURL function for check file exist:

<?php
$ch
= curl_init("//www.example.com/favicon.ico");

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode >= 400 -> not found, $retcode = 200, found.
curl_close($ch);
?>
up
-23
kalidass dot jst at gmail dot com
3 years ago
public function get_url($request_url) {

$curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $request_url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($curl_handle, CURLOPT_TIMEOUT, 0);
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($curl_handle, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
    $JsonResponse = curl_exec($curl_handle);
    $http_code = curl_getinfo($curl_handle);

  return($JsonResponse);
}
get_url("//www.example.com");
up
-63
heck at fas dot harvard dot edu
14 years ago
The previous post is part right, part wrong. It's part right because it's true that the php script will run on the remote server, if it's capable of interpreting php scripts. You can see this by creating this script on a remote machine:
<?php
echo system("hostname");
?>
Then include that in a php file on your local machine. When you view it in a browser, you'll see the hostname of the remote machine.

However, that does not mean there are no security worries here. Just try replacing the previous script with this one:
<?php
echo "<?php system(\"hostname\"); ?>";
?>
I'm guessing you can figure out what that's gonna do.

So yes, remote includes can be a major security problem.
To Top
  • 股票买卖中不可忽略的大宗交易数据(上) 2019-02-19
  • "央企暖男"与108名抗战老兵:向他们致以年轻一代的敬意 2019-02-19
  • 学生补课累到不行 网友:节假日都不休息 2019-02-19
  • 世界杯期间夜猫子吃什么好 这是有讲究的 2019-02-19
  • 一切腐败分子和一切为钱的各种违法犯罪高发:1、不是来自从无偿占有一个鸡蛋私心开始,而是从私有制社会存开始,因为占有他人一个鸡蛋的私有观念欲望,是从社会存在产生派 2019-02-18
  • 推进58个重点项目 杭州加快钱塘江金融港湾建设 2019-02-18
  • 高清:C罗帽子戏法科斯塔梅开二度 葡萄牙3 2019-02-18
  • 美国再挑贸易战,中方强力回击,全球市场跌声一片 2019-02-17
  • 内政部长威胁“单飞”,联盟党闹分裂,默克尔或下台? 2019-02-17
  • 看来“无名小卒也”这样的网民在公有制企业里有一大批,那么公有制企业一定会发展的快,搞的好,呵呵。 2019-02-17
  • 视频:太原蒙山景区举办首届蒙山春节庙会 2019-02-16
  • 习近平会见美国国务卿蓬佩奥 2019-02-16
  • [酷]中国天翻地复的变化确实惊人 2019-02-15
  • 王烜:当心单边主义在全球圈粉 2019-02-15
  • 端午假期虎门大桥最易拥堵 2019-02-15
  • 203| 755| 361| 783| 875| 176| 643| 686| 735| 13|