zoukankan      html  css  js  c++  java
  • 登录功能验证处理

    今天给大家带来一个简单的登陆验证,用的是ASIHttpRequest 这个开源类库,使用的方法很简单,从网上下载下来以后,添加到项目中,并添加一下这些框架。

     

    下面上代码

    1. //   
    2. //  ViewController.h   
    3. //  NetDemo   
    4. //   
    5. //  Created by zhouhaifeng on 12-6-6.   
    6. //  Copyright (c) 2012年 zhouhaifeng. All rights reserved.   
    7. //   
    8.   
    9. #import <UIKit/UIKit.h>   
    10. #import "ASIHttpHeaders.h"   
    11. #import "CJSONDeserializer.h"   
    12. #import "tooles.h"   
    13. @interface ViewController : UIViewController<ASIHTTPRequestDelegate>  
    14. {  
    15.     UITextField *username;  
    16.     UITextField *password;  
    17.   
    18. }  
    19. @property (nonatomic,retain) UITextField *username;  
    20. @property (nonatomic,retain) UITextField *password;  
    21.   
    22. @end  
    //
    //  ViewController.h
    //  NetDemo
    //
    //  Created by zhouhaifeng on 12-6-6.
    //  Copyright (c) 2012年 zhouhaifeng. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import "ASIHttpHeaders.h"
    #import "CJSONDeserializer.h"
    #import "tooles.h"
    @interface ViewController : UIViewController<ASIHTTPRequestDelegate>
    {
        UITextField *username;
        UITextField *password;
    
    }
    @property (nonatomic,retain) UITextField *username;
    @property (nonatomic,retain) UITextField *password;
    
    @end
    
    1. //   
    2. //  ViewController.m   
    3. //  NetDemo   
    4. //   
    5. //  Created by zhouhaifeng on 12-6-6.   
    6. //  Copyright (c) 2012年 zhouhaifeng. All rights reserved.   
    7. //   
    8.   
    9. #import "ViewController.h"   
    10. #define HOSTURL @"http://192.168.1.105/NetDemo/index.php";   
    11.   
    12.   
    13. @interface ViewController ()  
    14. -(void) login:(id)sender;  
    15. -(void) GetErr:(ASIHTTPRequest *)request;  
    16. -(void) GetResult:(ASIHTTPRequest *)request;  
    17. @end  
    18.   
    19. @implementation ViewController  
    20. @synthesize username,password;  
    21. - (void)viewDidLoad  
    22. {  
    23.     [super viewDidLoad];  
    24.     // Do any additional setup after loading the view, typically from a nib.   
    25.     UILabel *txt1 = [[UILabel alloc] initWithFrame:CGRectMake(30,100,70,30)];  
    26.     [txt1 setText:@"用户名"];  
    27.     [txt1 setBackgroundColor:[UIColor clearColor]];  
    28.     [self.view addSubview:txt1];  
    29.       
    30.     UILabel *txt2 = [[UILabel alloc] initWithFrame:CGRectMake(30,140,70,30)];  
    31.     [txt2 setText:@"密   码"];  
    32.     [txt2 setBackgroundColor:[UIColor clearColor]];  
    33.     [self.view addSubview:txt2];  
    34.       
    35.     username = [[UITextField alloc]initWithFrame:CGRectMake(120,100, 150, 30)];  
    36.     [username setBorderStyle:UITextBorderStyleRoundedRect];  
    37.     [self.view addSubview:username];  
    38.       
    39.     password = [[UITextField alloc]initWithFrame:CGRectMake(120,140, 150, 30)];  
    40.     [password setBorderStyle:UITextBorderStyleRoundedRect];  
    41.     [password setSecureTextEntry:YES];  
    42.     [self.view addSubview:password];  
    43.       
    44.     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
    45.     [btn setTitle:@"提交" forState:UIControlStateNormal];  
    46.     [btn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];  
    47.     [btn setFrame:CGRectMake(90, 180, 150, 40)];  
    48.     [self.view addSubview:btn];  
    49.       
    50.   
    51.       
    52. }  
    53.   
    54. -(void) login:(id)sender  
    55. {  
    56.     //表单提交前的验证   
    57.     if (username.text == nil||password.text==nil ) {  
    58.         [tooles MsgBox:@"用户名或密码不能为空!"];  
    59.         return;  
    60.     }  
    61.     //隐藏键盘   
    62.     [username resignFirstResponder];  
    63.     [password resignFirstResponder];  
    64.     //   
    65.     [tooles showHUD:@"正在登陆...."];  
    66.     NSString *urlstr = HOSTURL;  
    67.     NSURL *myurl = [NSURL URLWithString:urlstr];  
    68.     ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myurl];  
    69.     //设置表单提交项   
    70.     [request setPostValue:username.text forKey:@"username"];      
    71.     [request setPostValue:username.text forKey:@"password"];  
    72.     [request setDelegate:self];  
    73.     [request setDidFinishSelector:@selector(GetResult:)];  
    74.     [request setDidFailSelector:@selector(GetErr:)];  
    75.     [request startAsynchronous];  
    76.       
    77.       
    78. }  
    79.   
    80.    //获取请求结果   
    81. - (void)GetResult:(ASIHTTPRequest *)request{      
    82.       
    83.     [tooles removeHUD];  
    84.     NSData *data =[request responseData];  
    85.     NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:nil];  
    86.     <P class=p1><SPAN class=s1>    </SPAN><SPAN class=s2>//</SPAN>输出接收到的字符串</P><P class=p2><SPAN class=s4>    NSString</SPAN><SPAN class=s3> *str = [</SPAN><SPAN class=s4>NSString</SPAN><SPAN class=s3> </SPAN>stringWithUTF8String<SPAN class=s3>:[data </SPAN>bytes<SPAN class=s3>]];</SPAN></P><P class=p3>    <SPAN class=s5>NSLog</SPAN>(<SPAN class=s6>@"%@"</SPAN>,str);</P><P class=p1><SPAN class=s1>    </SPAN><SPAN class=s2>//</SPAN>判断是否登陆成功</P>   
    87.     if ([dictionary objectForKey:@"yes"]) {  
    88.         [tooles MsgBox:[dictionary objectForKey:@"yes"]];  
    89.         return;  
    90.     }else if ([dictionary objectForKey:@"error"] != [NSNull null]) {  
    91.         [tooles MsgBox:[dictionary objectForKey:@"error"]];  
    92.         return;  
    93.     }  
    94.       
    95. }  
    96.   
    97.   //连接错误调用这个函数   
    98. - (void) GetErr:(ASIHTTPRequest *)request{  
    99.       
    100.     [tooles removeHUD];  
    101.       
    102.     [tooles MsgBox:@"网络错误,连接不到服务器"];  
    103. }  
    104.   
    105. - (void)viewDidUnload  
    106. {  
    107.     [super viewDidUnload];  
    108.     // Release any retained subviews of the main view.   
    109. }  
    110.   
    111. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
    112. {  
    113.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
    114. }  
    115.   
    116. -(void) dealloc  
    117. {  
    118.     [username release];  
    119.     [password release];  
    120.     [super dealloc];  
    121. }  
    122.   
    123. @end  
    //
    //  ViewController.m
    //  NetDemo
    //
    //  Created by zhouhaifeng on 12-6-6.
    //  Copyright (c) 2012年 zhouhaifeng. All rights reserved.
    //
    
    #import "ViewController.h"
    #define HOSTURL @"http://192.168.1.105/NetDemo/index.php";
    
    
    @interface ViewController ()
    -(void) login:(id)sender;
    -(void) GetErr:(ASIHTTPRequest *)request;
    -(void) GetResult:(ASIHTTPRequest *)request;
    @end
    
    @implementation ViewController
    @synthesize username,password;
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
        UILabel *txt1 = [[UILabel alloc] initWithFrame:CGRectMake(30,100,70,30)];
        [txt1 setText:@"用户名"];
        [txt1 setBackgroundColor:[UIColor clearColor]];
        [self.view addSubview:txt1];
        
        UILabel *txt2 = [[UILabel alloc] initWithFrame:CGRectMake(30,140,70,30)];
        [txt2 setText:@"密   码"];
        [txt2 setBackgroundColor:[UIColor clearColor]];
        [self.view addSubview:txt2];
        
        username = [[UITextField alloc]initWithFrame:CGRectMake(120,100, 150, 30)];
        [username setBorderStyle:UITextBorderStyleRoundedRect];
        [self.view addSubview:username];
        
        password = [[UITextField alloc]initWithFrame:CGRectMake(120,140, 150, 30)];
        [password setBorderStyle:UITextBorderStyleRoundedRect];
        [password setSecureTextEntry:YES];
        [self.view addSubview:password];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setTitle:@"提交" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
        [btn setFrame:CGRectMake(90, 180, 150, 40)];
        [self.view addSubview:btn];
        
    
        
    }
    
    -(void) login:(id)sender
    {
        //表单提交前的验证
        if (username.text == nil||password.text==nil ) {
            [tooles MsgBox:@"用户名或密码不能为空!"];
    		return;
        }
        //隐藏键盘
        [username resignFirstResponder];
        [password resignFirstResponder];
        //
        [tooles showHUD:@"正在登陆...."];
    	NSString *urlstr = HOSTURL;
    	NSURL *myurl = [NSURL URLWithString:urlstr];
    	ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myurl];
    	//设置表单提交项
    	[request setPostValue:username.text forKey:@"username"];	
    	[request setPostValue:username.text forKey:@"password"];
    	[request setDelegate:self];
    	[request setDidFinishSelector:@selector(GetResult:)];
    	[request setDidFailSelector:@selector(GetErr:)];
    	[request startAsynchronous];
        
    	
    }
    
       //获取请求结果
    - (void)GetResult:(ASIHTTPRequest *)request{	
    	
    	[tooles removeHUD];
        NSData *data =[request responseData];
        NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:nil];

      //输出接收到的字符串

    NSString *str = [NSStringstringWithUTF8String:[data bytes]];

        NSLog(@"%@",str);

        //判断是否登陆成功

        if ([dictionary objectForKey:@"yes"]) {
    		[tooles MsgBox:[dictionary objectForKey:@"yes"]];
    		return;
        }else if ([dictionary objectForKey:@"error"] != [NSNull null]) {
    		[tooles MsgBox:[dictionary objectForKey:@"error"]];
    		return;
    	}
    	
    }
    
      //连接错误调用这个函数
    - (void) GetErr:(ASIHTTPRequest *)request{
        
        [tooles removeHUD];
        
        [tooles MsgBox:@"网络错误,连接不到服务器"];
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    -(void) dealloc
    {
        [username release];
        [password release];
        [super dealloc];
    }
    
    @end
    

     

    php端验证的代码,随便写了下,后面就是返回一个JSON格式的字符串。

    1. <?php  
    2.     if($_POST['username'] == "admin" &&  $_POST['password'] == "admin")  
    3.     {  
    4.         echo '{"yes":"登陆成功"}';  
    5.     }else  
    6.     {  
    7.         echo '{"error":"用户名或密码错误"}';  
    8.     };  
    9. ?>  
    <?php
    	if($_POST['username'] == "admin" &&  $_POST['password'] == "admin")
    	{
    		echo '{"yes":"登陆成功"}';
    	}else
    	{
    		echo '{"error":"用户名或密码错误"}';
    	};
    ?>



  • 相关阅读:
    反射 Reflect Modifier 修饰符工具类
    【泛型】Generic 参数化类型 类型转换
    泛型 Generic 类型擦除引起的问题及解决方法
    注解 Annotation 简介 总结 MD
    反射 Reflect Class 基础 API MD
    【Type】类型 ParameterizedType
    Java中浮点类型的精度问题 double float
    jQuery之$.ajax()方法详解及实例
    pace.js – 加载进度条插件
    在线代码编辑、保存与运行网址推荐
  • 原文地址:https://www.cnblogs.com/yulang314/p/3568645.html
Copyright © 2011-2022 走看看