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

标签 php ios string swift hash

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

在我的应用程序中,我配置了一个类似这样的字符串:name=John&pass=apricot&constKey=12345

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

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

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

然后我对该字符串进行哈希处理并将其与 localKey 进行比较,localKey 是 blablabla25。如果 key 相等,则一切正常。在大多数情况下它工作正常。

但有时我会发送,例如: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 中默认启用),您需要对 $ 中的任何变量调用 stripslashes _POST$_GET

参见 http://php.net/manual/en/function.stripslashes.php

关于php - 向 PHP 脚本发送请求时出现错误符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36758320/

相关文章:

php - 在 Laravel MongoDB 中显示 EmbedsMany 数据

php - 帮助理解以下框架如何组合在一起 :

iOS + 网络服务 + 内联网

c++ - 在 C++ 中高效传递字符串文字

c - 将 dvb url 地址拆分为单独的值

javascript - 循环数据表行

php - 使用带有php mysql的单个复选框在一行中插入多条记录

ios - 通过 PowerPoint 打开 iOS 应用程序

ios - 在 UICollection 数据源读取响应之前,如何处理来自 API 的响应?

java - Part 类型中的方法 setText(String) 不适用于参数