写完注册,继续把登录也写了吧,例子知识简单介绍,实际开发应更加严谨
- (IBAction)loginClick:(id)sender
{
NSString *user = [self.username.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *pass = [self.password.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if(![user isEqualToString:@""]){
NSDictionary *par = @{@"username":user,@"password":pass};
[[netTool shareDL]postDataFromUrlString:@"http://192.168.1.102/myphp/login.php" parameter:par progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"成功:%@",responseObject);
} fail:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}
}
$username = $_POST['username'];
$password = $_POST['password'];
$mysqli = new mysqli("127.0.0.1","root","","demo");
if($mysqli->connect_error){
echo "连接失败".$mysqli->connect_error;
}
$mysqli->query("set names utf8");
$sql = "select * from myuser where username='$username' and password='$password'";
$res = $mysqli->query($sql);
if($res->num_rows){
$result = array();
$result['code'] = "1";
$result['des'] = "登陆成功";
$result['list'] = array();
while ($row=$res->fetch_assoc()) {
$list = array();
foreach ($row as $key => $val) {
$list[$key] = $val;
}
array_push($result['list'],$list);
}
echo json_encode($result);
}else{
$result=array();
$result['code']="-1";
$result['des']="登录失败";
echo json_encode($result);
}
login.gif
网友评论