简体   繁体   English

Xcode中收到的PHP回声。 有隐藏的字符吗?

[英]PHP echo received in Xcode. Are there hidden characters?

I have a PHP script on a server, all working fine for most of the thing I need it to do. 我在服务器上有一个PHP脚本,对于我需要它执行的大多数操作,都可以正常工作。 I'm now setting up special user privileges for certain users. 我现在为某些用户设置特殊的用户特权。

I check if the logged in user is registered on a special user database, then return the name of their privleges or 'blank' using the following: 我检查登录用户是否已在特殊用户数据库上注册,然后使用以下命令返回其特权或“空白”的名称:

private function checkSpecialUser($userID)
{
    $db = new DbConnect();
    $result = $db->query("SELECT privelege FROM special_users WHERE userID = $userID");
    if ($privName = $result->fetch_object())
    {
        echo $privName->privelege;
    }
    else
    {
        echo 'blank';
    }
    $db->close();
}

In Xcode, I have set up a simple function to return the string which is returned by the PHP script (I'll leave out the connection part since that all works fine): 在Xcode中,我设置了一个简单的函数来返回由PHP脚本返回的字符串(由于一切正常,我将省略连接部分):

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&requestError];

NSString *packName = (NSString *)[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
packName = [packName stringByReplacingOccurrencesOfString:@" " withString:@""];

NSLog(@"packName is %@", packName);

if([packName isEqualToString:@"blank"])
{
    NSLog(@"user is not special");
}
else
{
    NSLog(@"User is special :-)");
}
return packName;

Now to the problem I have. 现在我有问题了。 If the user does not have special privileges then the first NSLog prints "packName is blank" which is exactly what I would expect. 如果用户没有特殊特权,则第一个NSLog将打印“ packName is blank”,这正是我所期望的。 However, the if statement should then pick up that packName is equalToString "blank", which it doesn't. 但是,如果使用if语句,则应将packName等于equalToString“ blank”,但不是。 It always reaches 'else' and prints "User is special :-)". 它总是到达“ else”并打印“ User is special :-)”。

I've double checked with users who ARE registered, and although it returns the string I would expect, again it doesn't trigger an equalToString response. 我已经与已注册的用户进行了双重检查,尽管它返回了我期望的字符串,但它不会触发equalToString响应。

Do PHP echoes have hidden characters in them that I would need to remove, or am I somehow getting the value from the database incorrectly? PHP回声中是否包含我需要删除的隐藏字符,还是我不知何故从数据库中获取了错误的值? In the database each row is simply a userID which is a varchar, and the name of their privilege which is also a varchar. 在数据库中,每一行只是一个userID(它是一个varchar),以及它们的特权名称(也是varchar)。

If anyone has any tips I'd be really grateful. 如果有人有任何建议,我将不胜感激。 Thanks. 谢谢。

Try, instead of sending back "blank", sending back nothing, or an empty string. 尝试,而不是发回“空白”,什么也不发回,或者发空字符串。 Then, instead of matching the word "blank", test the length of the string. 然后,不匹配单词“ blank”,而是测试字符串的长度。 This will at least tell you if there are other characters in packName ... you might remove more than just space whitespace, if I had to guess I'd say you've got a newline in there. 这至少会告诉您packName是否还有其他字符……如果我不得不猜测我会说您那里有换行符,那么您可能会删除多个空格而不只是空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM