我想制作一个具有loginViewController的iPhone应用程序。此代码正确且安全吗?
-(IBAction)loginPressed:(id)sender{
NSString *usr = [[NSString alloc]init];
NSString *psw = [[NSString alloc]init];
usr = username.text;
psw = password.text;
NSLog(@"%@",usr);
NSLog(@"%@", psw);
NSString *URL = [NSString stringWithFormat:@"http://www.mywebsite.com/loginApp.php?user_name=%@&password=%@", usr, psw];
NSLog(@"%@", URL);
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:URL]];
NSString *Result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if ([Result isEqualToString:@"1"])
{
NSLog(@"logged in");
}else if ([Result isEqualToString:@"2"]){
UIAlertView *WrongCredential = [[UIAlertView alloc]initWithTitle:@"Loggin failed" message:@"sorry, check again your email and password" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles: nil];
[WrongCredential show];
}
}
loginApp.php
<?php
require_once 'functions/init.php';
if (isset($_GET['user_name'], $_GET['password'])){
$email = sanitize($_GET['user_name']);
$password = sanitize($_GET['password']);
$login = loginApp($email, $password);
if($login === true){
echo json_encode(1);
}
else if($login === false){
echo json_encode(2);
}
}
?>
如何检查用户是否已经登录?在php中,我可能会设置一个session / cookies(?),对吗?我该如何使用Objective C?
最佳答案
维护一个标志或变量(比如说bool-isUserLoggedIn)。默认值为NO,
if ([Result isEqualToString:@"1"])
{
NSLog(@"logged in");
isUserLoggedIn = YES;
}else if ([Result isEqualToString:@"2"]){
UIAlertView *WrongCredential = [[UIAlertView alloc]initWithTitle:@"Loggin failed" message:@"sorry, check again your email and password" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles: nil];
[WrongCredential show];
isUserLoggedIn = NO;
}
现在,您可以随时使用此标志值(isUserLoggedIn)进行检查
希望这就是您要寻找的
关于php - iOS登录-此代码正确/安全吗?如何检查用户是否已经登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15503242/