gpt4 book ai didi

php - 尝试通过将用户名/密码发送到 PHP 脚本来验证 iOS 中的用户

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:19 25 4
gpt4 key购买 nike

我正在尝试通过从我的 iOS 应用向 php 脚本发送 POST 请求来验证用户身份。然后让 iOS 应用程序读取 NSHTTPURLResponse 以确定它是否成功。我的 iOS 代码如下所示,出于某种原因,它说我的 NSURLConnection 未使用。 int 代码总是返回 200 的值。

- (void)authenticateUser:(NSString *)aUsername
password:(NSString *)aPassword
{
NSString *post = [NSString stringWithFormat:@"username=%@&pw=%@", aUsername, aPassword];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [post length]];

NSURL *url = [NSURL URLWithString:@"http://XXX.com/query-db.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int code = [httpResponse statusCode];

// Log the response
NSLog(@"%d", code);
if(code == 1){
NSLog(@"received a 1");
[self updateWorkTime];
//[self close];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unsuccessul Attempt" message:@"The username or password is invalid, please try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}

此外,如果您想看一下我的 PHP 脚本,我在虚拟变量中输入了它并进行了测试,看它是否正确地查询了数据库并检查了...但以防万一您好奇。

<?php
$sentUsername = $_POST['username'];
$sentPW = $_POST['pw'];

mysql_connect("xxx", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());

$result = mysql_query("SELECT * FROM Table") or die(mysql_error());

$authenticate = 0;

while($authenticate == 0) {
$row = mysql_fetch_array($result);
$selectedUsername = $row['username'];
$selectedPW = $row['pw'];
$pwComp = strcmp($sentPW, $selectedPW);
$userComp = strcmp($selectedUsername, $sentUsername);

if(!$row) break;
if($selectedPW == $sentPW && $selectedUsername == $sentUsername) {
$authenticate = 1;
}
}
echo $authenticate;
?>

我尝试设置 PHP header ,但不确定如何设置。我感谢这个社区给我的所有帮助,这真的是无价之宝。谢谢!

最佳答案

您返回的“200”代码是 HTTP 状态代码,表示请求成功。

如果您在从 PHP 脚本发回“1”或“0”值之后,您应该在 didReceiveData: 委托(delegate)方法中查找它,而不是在 didReceiveResponse:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
NSLog(@"authenticate value => %@",[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]);
}

关于php - 尝试通过将用户名/密码发送到 PHP 脚本来验证 iOS 中的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9407804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com