gpt4 book ai didi

php - Xcode 中使用 php 和 mysql 的登录程序无法正常工作

转载 作者:行者123 更新时间:2023-11-29 13:12:07 24 4
gpt4 key购买 nike

嗨,我正在我的应用程序中执行登录页面。我在控制台中收到错误和无效消息。帮助我得到罚款结果。提前致谢。我的 Xcode 在这里

- (IBAction)login:(id)sender {

NSInteger success = 0;
@try {

if([[self.username text] isEqualToString:@""] || [[self.password text] isEqualToString:@""] ) {

[self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0];

} else {
NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[self.username text],[self.password text]];
NSLog(@"PostData: %@",post);

NSURL *url=[NSURL URLWithString:@"http://192.168.1.5:81/priya/login.php"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSLog(@"Response code: %ld", (long)[response statusCode]);

if ([response statusCode] >= 200 && [response statusCode] < 300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);

NSError *error = nil;
NSDictionary *jsonData = [NSJSONSerialization
JSONObjectWithData:urlData
options:NSJSONReadingMutableContainers
error:&error];

success = [jsonData[@"success"] integerValue];
NSLog(@"Success: %ld",(long)success);

if(success == 1)
{
NSLog(@"Login SUCCESS");
} else {

NSString *error_msg = (NSString *) jsonData[@"error_message"];
[self alertStatus:error_msg :@"Sign in Failed!" :0];
}

} else {
//if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0];
}
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Sign in Failed." :@"Error!" :0];
}
if (success) {
[self performSegueWithIdentifier:@"login_success" sender:self];
}}

和我的 php 编码

<?php

$host = "localhost";
$user = "xcode";
$pass = "xcode";
$db="xcode";

$r = mysql_connect($host, $user, $pass);

if (!$r) {
echo "Could not connect to server\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Connection established\n";
}

echo mysql_get_server_info() . "\n";
$r2 = mysql_select_db($db);

if (!$r2) {
echo "Cannot select database\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "database selected\n";
}
$u=$_POST['username'];
$pw=$_POST['password'];
$result=mysql_query("select username,password from login where username='$u' and password='$pw'");

$row=mysql_fetch_array($result);
if($row["username"]==$u && $row["password"]==$pw)
{
echo '{"success":1}';
}
else
{
'{"success":0,"error_message":"Username and/or password is invalid."}';
}

mysql_close();
?>

我在控制台中得到了结果

2014-02-22 13:44:48.172 school apps[1605:a0b] PostData: username=admin&password=xcode
2014-02-22 13:44:49.252 school apps[1605:a0b] Response code: 200
2014-02-22 13:44:49.252 school apps[1605:a0b] Response ==> Connection established
5.5.34
database selected
{"success":1}
2014-02-22 13:44:49.253 school apps[1605:a0b] Success: 0

如果有任何教程,请给我 url。我对 Xcode 很陌生。

最佳答案

您需要将mysql数据库输出转换为json格式。

   //set the mime type
$document->setMimeEncoding('application/json');
echo json_encode($yourdata);

转换后,您需要

    jexit();

排除源代码中的 html。

关于php - Xcode 中使用 php 和 mysql 的登录程序无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21951671/

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