I have a PHP script on a server, all working fine for most of the thing I need it to do. 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):
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. However, the if statement should then pick up that packName is equalToString “blank”, which it doesn’t. It always reaches ‘else’ and prints “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.
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? In the database each row is simply a userID which is a varchar, and the name of their privilege which is also a 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. 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.