PHP后台实现IOS/OC_App于服务器端登陆验证(Demo)

PHP代码部分

  • login.php
      <?php 
class UsersLogin {
    // 用户登录
    function userLogin() {
        if (isset($_GET['username']) && isset($_GET['password'])) {
            // 获取GET请求参数
            $accessType = '[GET]';
            $userName = $_GET['username'];
            $userPassword = $_GET['password'];
        } else if (isset($_POST['username']) && isset($_POST['password'])) {
            // 获取POST请求参数
            $accessType = '[POST]';
            $userName = $_POST['username'];
            $userPassword = $_POST['password'];
        } else {
            echo('非法请求。');
            return false;
        }

        if ($userName == 'Messi' && $userPassword == 'Winner') {
            // 将查询结果绑定到数据字典
            $result = array(
                            'userId' => 1,
                            'userName' => 'Messi'
                            );
            // 将数据字典使用JSON编码
            echo json_encode($result);
        } else {
            $result = array(
                            'userId' => 0,
                            'userName' => ''
                            );

            echo json_encode($result);
        }

        return true;
    }
}

header('Content-Type:application/json;charset=utf-8');
$UsersLogin = new Users;
$UsersLogin->userLogin();
?>

OC代码部分

  • ViewController.m
//
//  ViewController.m
//  PHP后台_
//
//  Created by Larry.Hwang on 15/7/10.
//  Copyright (c) 2015年 Larry.Hwang. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <NSURLConnectionDataDelegate>

@property (weak , nonatomic) UITextField *nameTextFiled;
@property (weak,  nonatomic) UITextField *passwordTextFiled;
@property (strong ,nonatomic) NSMutableData *ServerData;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];

}

- (void)setupUI {
    NSMutableArray *arrayM =[[NSMutableArray alloc]initWithCapacity:4];
    for (int i=0; i<2; i++) {
        UITextField *text  =  [[UITextField alloc]initWithFrame:CGRectMake(75, 40+40*i, 200, 30)];
        [text setBorderStyle:UITextBorderStyleRoundedRect];  //边框设定
        [text setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];  //文字剧中
        [arrayM addObject:text];
        [self.view addSubview:text];
    }


    for (int i=0; i<2; i++) {
        UILabel *lable  =  [[UILabel alloc]initWithFrame:CGRectMake(15, 33+40*i, 60, 40)];
        [arrayM addObject:lable];
        [self.view addSubview:lable];
    }
    [(UILabel *)arrayM[2] setText:@"用户名"];   //数组里面的成员对象调用方法
    [(UILabel *)arrayM[3] setText:@"密  码"];


    self.nameTextFiled = arrayM[0];
    self.passwordTextFiled = arrayM[1];

   [self.passwordTextFiled setSecureTextEntry:YES];   //密码星号


    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setFrame:CGRectMake(130, 140, 60, 30)];    //

    [btn setTitle:@"登陆" forState:UIControlStateNormal];
    btn.layer.borderWidth =1.5;

   btn.layer.cornerRadius = 4.5;
    // 设置颜色空间为rgb,用于生成ColorRef

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    // 新建一个红色的ColorRef,用于设置边框(四个数字分别是 r, g, b, alpha)
    CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){0.6824f, 0.7882f, 1.0f, 1});
    // 设置边框颜色
    btn.layer.borderColor = borderColorRef;

    [self.view addSubview:btn];

    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];


}


-(void) click {

    //建立请求
    NSString  *name  = self.nameTextFiled.text;
    NSString  *pwd = self.passwordTextFiled.text;
    NSString *urlString  = [NSString stringWithFormat:
 @"http://localhost/IOS_Server/login.php?username=%@password=%@",name,pwd];  
        // 在上面修改自己的具体地址,注意你Mac下的PHP环境是否开启
        //若URL中包含中文字符 ,则需要转换为百分号的格式 ,都使用UTF8编码
    NSLog(@"%@",urlString);
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:urlString];    //字符串转URL
         //建立请求
    NSURLRequest  *request = [NSURLRequest  requestWithURL:url];

          // 建立连接
    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];

          //启动连接,异步的  执行后  马上 执行下一句
    [conn start];
          //初始化数据
    self.ServerData = [NSMutableData data];    

}



#pragma  mark  -代理方法

    // 1.得到接到服务器的响应第一个执行的方法,服务器要传送数据   初始化接受过来的数据
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

}

     //2.接受服务器数据
-(void)connection:(NSURLConnection *) connection didReceiveData:(NSData *)data {
    [self.ServerData  appendData:data];    //不断接受数据,可能多次执行
}

     //3.数据接受完成后,做后续处理
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"has receive data");
    NSString *str = [[NSString alloc]initWithData:self.ServerData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);    //打印服务器返回信息

    UIAlertView *alertView = self.ServerData = nil;   //清理数据
}

     //4.获取网络错误时的信息
-(void)connection:(NSURLConnection *) connection diFailWithError : (NSError *) error{
    NSLog(@"网络请求错误:%@",error.localizedDescription);
}





@end



运行结果:
界面
在分别在用户名和密码框输入 :Messi 和 Winner
Xcode输出台输出:
这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值