PHP Velho Oeste 2024

Uzak dosyaların kullanımı

php.ini içinde allow_url_fopen seçeneği etkin olduğu sürece, dosya adını bağımsız değişken olarak alan birçok işlevde HTTP ve FTP URL'lerini kullanabilirsiniz. Ek olarak, URL'ler include, include_once, require ve require_once deyimleri ile kullanılabilir (Bunun için allow_url_include seçeneğini etkinleştirmek gerekmektedir). PHP tarafından desteklenen protokoller hakkında daha fazla bilgi için bakınız: Desteklenen Protokoller ve Sarmalayıcılar

Örneğin, bunu uzak web sunucusunda bir dosya açmak, istediğiniz veri için çıktıyı çözümlemek ve bu veriyi bir veritabanı sorgusunda kullanmak veya sadece çıktılamak için kullanabilirsiniz.

Örnek 1 - Uzak sayfanın başlığını elde etmek

<?php
$dosya
= fopen ("http://www.ornek_bir_site.com/", "r");
if (!
$dosya) {
echo
"<p>Uzak dosya açılamıyor.\n";
exit;
}
while (!
feof ($dosya)) {
$satir = fgets ($dosya, 1024);
/* Bu sadece başlığın title etiketleri aynı satır üzerindeyse çalışır */
if (preg_match ("@\<title\>(.*)\</title\>@i", $satir, $cikti)) {
$baslik = $cikti[1];
break;
}
}
fclose($dosya);
?>

Aynı zamanda bir FTP sunucusundaki dosyalara da yazabilirsiniz (doğru erişim haklarına sahip bir kullanıcı olarak bağlandığınızda). Bu yöntemi kullanarak sadece yeni dosyalar oluşturabilirsiniz; eğer varolan bir dosyanın üzerine yazmayı denerseniz, fopen() çağrısı başarısız olur.

'anonymous' haricinde bir kullanıcı olarak bağlanmak için, 'ftp://kullanici:parola@ftp.ornek_bir_site.com/ornek/bir/dosya' örneğindeki gibi kullanıcı adı (ve parola) URL içinde belirtilmelidir. (Basit kimlik doğrulaması gerektiren HTTP dosya erişimleri içinde aynı söz dizimini kullanabilirsiniz.)

Örnek 2 - Veriyi uzak sunucuda saklamak

<?php
$dosya
= fopen ("ftp://ftp.ornek_bir_site.com/gelen/yazilan_dosya", "w");
if (!
$dosya) {
echo
"<p>Uzak dosya yazmak için açılamıyor.\n";
exit;
}
/* Veriyi burada yaz. */
fwrite ($dosya, $_SERVER['HTTP_USER_AGENT'] . "\n");
fclose ($dosya);
?>

Bilginize:

Yukarıdaki örnek, bu tekniğin uzak günlük dosyası yazmak için kullanılabileceği fikrini vermiş olabilir. Ne yazık ki bu çalışmaz çünkü eğer uzak dosya zaten varsa fopen() çağrısı başarısız olur. Böyle bir dağıtık günlüklerle için syslog() işlevine göz atmalısınız.

add a note add a note

User Contributed Notes 4 notes

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

<?php
$ch
= curl_init("http://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
-4
anon
4 years ago
The following functions can't access external files if allow_url_fopen is disabled:

- fopen
- file_put_contents
- copy

I don't know if this is documented anywhere else, but I couldn't find it. Feel free to delete this post if so.
up
-33
kalidass dot jst at gmail dot com
8 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("http://www.example.com");
up
-70
heck at fas dot harvard dot edu
19 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