向PHP脚本发送请求时出现错误符号


Wrong symbols when sending Request to PHP Script

我从应用程序向网站上的PHP脚本发送请求。

在我的应用程序中,我配置了一个字符串,如下所示:name=John&pass=apricot&constKey=12345

然后,我在该字符串上使用hash,并收到类似这样的内容:blablabla25。现在是localKey。

然后我将最后一个字符串name=John&pass=apricot&localKey=blablabla25发送到PHP脚本。

在我的PHP脚本中,我使用除了localKey(name=John&pass=apricot)之外的这个字符串,并添加存储在PHP文件中的constKey。我再次得到字符串name=John&pass=apricot&constKey=12345。这很好。

然后我对该字符串进行散列,并将其与localKey(即blablabla25)进行比较。如果键相等,那么一切都好。在大多数情况下,它运行良好。

但有时我会发送,例如:text=I'm happy.That's why I'm dancing。我收到错误,因为当我在Swift中散列字符串时,它看起来是一样的。当我在PHP中对该字符串进行散列时,撇号会被这样的东西取代——''''

以下是我为Swift和PHP文件编写的部分代码。

post = post + "key='(sign)"
    post = post.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!
    post = post.stringByReplacingOccurrencesOfString("+", withString: "%2B")
    let postData = post.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
    let postLength = "'(postData?.length)"
    let url: NSURL = NSURL(string: "http://google.com/admin.php")!
    let request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
    request.URL = url
    request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
    request.HTTPMethod = "POST"
    request.setValue(postLength, forHTTPHeaderField: "Content-Length")
    request.setValue("application/x-www-form-urlencoded" , forHTTPHeaderField: "Content-Type")
    request.HTTPBody = postData

和PHP文件

$key = NULL;
if (isset($POST['key'])) $key = mysql_real_escape_string($_POST['key']);
if (!$key)
    {
        exit();
    }
    $login = NULL;
    $pass = NULL;
    //////////////////////////////////////////////////////////////////////////////////////////
    $pars = array();
    foreach ($_POST as $par => $val)
    {
        $pars[$par] = mysql_real_escape_string($val);
    }
    ksort($pars);
    $str = "";
    foreach ($pars as $pr => $vl)
    {
        if ($pr != 'key') $str .= $pr . '=' . $vl;
    }
    $str .= genkey;
    $sign = md5($str);
    if (strcmp($key, $sign)  !== 0)
    {
        retError(err_unknown, 'Unknown error');
        exit();
    }

我知道MD5现在不好,但这不是问题的重点。

PHP更新代码

    $key = NULL;
    if (isset($_POST['key'])) $key = $_POST['key'];
    if (!$key)
    {
        exit();
    }
    $login = NULL;
    $pass = NULL;
    //////////////////////////////////////////////////////////////////////////////////////////
    $pars = array();
    foreach ($_POST as $par => $val)
    {
        $pars[$par] = $val;
    }
    ksort($pars);
    $str = "";
    foreach ($pars as $pr => $vl)
    {
        if ($pr != 'key') $str .= $pr . '=' . $vl;
    }
    $str .= genkey;
    $sign = md5($str);
    if (strcmp($key, $sign)  !== 0)
    {
        exit();
    }

字符串,我发送到PHP脚本:

'Suggs
'H
Fgdfh'dg'hd'hgd

字符串,即在PHP:中

'''Suggs'n'''H'nFgdfh'''dg'''hd'''hgd

所以,新行是'n,撇号是'''当我在swift中散列字符串时,没有'n'''。这就是散列不同的原因。我可以简单地从字符串中删除新行和撇号,但这不是最好的解决方案,我认为

如果您的PHP配置中打开了magic_quotes_gpc(在PHP<5.4中默认打开),则需要对$_POST$_GET中的任何变量调用stripslashes

请参阅http://php.net/manual/en/function.stripslashes.php

您对该字符串的转义太快(这是chnaging"to ''"的原因)。顺便说一句,在PHP 5.5.0 中,只使用一些PDO或MySQLi mysql_扩展已被弃用

更新代码以从查询中剪切密钥=xxx部分

$dataArray=$_POST;

if (isset($dataArray['key'])){
    unset($dataArray['key']);
}
echo http_build_query($dataArray);