php - 为 file_get_contents 清理用户提供的 URL

标签 php proxy file-get-contents

我想使用 file_get_contents 来实现代理,这样我就可以执行跨域 AJAX 请求。

查询字符串将用于为 file_get_contents 提供 URL。现在的问题是人们可以随意使用查询字符串来读取服务器上的本地文件。我不想要这个。有人可以给我一个函数来清理查询字符串,以便只接受 URL 而不是本地文件,即:

  • ?url=http://google.com.au - 确定

  • ?url=./passwords.txt - 不正常

最佳答案

$url = filter_var($_GET['url'], FILTER_SANITIZE_URL);

if($_GET['url'] === filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
    ... your stuff here ...
}

关于php - 为 file_get_contents 清理用户提供的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2957038/

相关文章:

php - 查找列中的最高值,在该列中插入一个比该列高 1 个数字的新记录

php - cakephp 1.2 获取 URL 参数

php - simplexml_load_file 不工作

php - 使用 file_get_contents 将 JSON 转换为 PHP 数组

php - Azure 身份验证 Symfony

php - 用户/密码身份验证 PHP/MySQL - PHP 或 MySQL 查询中的比较?

jquery ajax 调用不起作用?!是firefox还是xss问题?

node.js - 在 Apache 代理后面运行多个 Node.js 服务器

go - 通过代理发出请求时为 "EOF"

php - 如何在 URL 中使用带有非英文符号的 file_get_contents()?