php - 保护 GET 请求的好方法?

标签 php ios api

我正在构建一个示例服务器,以便我可以学习 PHP,并且想知道这是否是保护我的数据库的好方法。本质上我正在发送我自己的 API key 。这是否做了任何有用的事情,或者这是否很容易克服。

如果这很可怕,那么解决这个问题的标准方法是什么?

下面是php文件

<?php
//Check if insert function exists
$apiKey = "6dM7V0n5GqYJLTMibQDf2gA2a94h8hbF";

if(function_exists($_GET['insert']) && ($_GET['key'])==$apiKey) {

    $id = $_GET['id'];
    $first=$_GET['first'];
    $last = $_GET['last'];

    //If found, call the function inser with value as a parameter
   $_GET['insert']($id,$first,$last);
}
?>

网络请求看起来像(来自 iOS 应用程序);

    NSURL*url=[NSURL URLWithString:@"http://localhost/Tutorials/index.php?insert=insert&id=9000&first=YOOOO&last=LOOOO&key=6dM7V0n5GqYJLTMibQDf2gA2a94h8hbF"];

    //URL request
    NSURLRequest*request=[NSURLRequest requestWithURL:url];

    //Set the connection
    connection = [NSURLConnection connectionWithRequest:request delegate:self];

    if (connection) {
        webData=[[NSMutableData alloc]init];
    }

最佳答案

为了证明它是多么不安全,我可以请求此页面:

http://example.com/yourpage.php?insert=exec&id=rm+-rf+%2F&key=6dM7V0n5GqYJLTMibQDf2gA2a94h8hbF

除非某些配置阻止它,否则这将删除您服务器的驱动器,只要 PHP 可以访问它(这可能是您的整个网站)

相反,您应该创建有效函数的列表,并通过 ID 引用它们。将函数存储在数组中,并获取 GET 参数指向的索引。

关于php - 保护 GET 请求的好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15712352/

相关文章:

node.js - OUTLOOK REST API 使用 Node.js 接受事件

mysql - 什么存储系统用于实时消息传递?

php - PHP 中的特殊字符(如 omega 和 beta)字符串匹配

ios - 在允许通知设置更改时收到通知 iOS swift

ios - 尝试将 ios 添加到项目时出现 Ionic 错误

ios - 将参数传递给 dispatch_async

node.js - 亚马逊产品 API 不尊重响应组

php - fatal error Joomla+Cron+Smart Search+Acepolls

php - xmlHTTPRequest POST 不发送数据

php - Laravel-4.1 时间戳()问题