使用 POST 将用户信息存储在 Web 服务器 IOS 应用程序中

Posted

技术标签:

【中文标题】使用 POST 将用户信息存储在 Web 服务器 IOS 应用程序中【英文标题】:using POST to store user information in web server IOS app 【发布时间】:2016-02-20 20:40:20 【问题描述】:

我正在使用 Swift + xcode7 + php 脚本 + mysql 服务器端 (Bluehost) 构建一个 ios 应用程序,这实际上是我使用 xcode 的第一个项目

当我尝试将用户信息存储到数据库中时,什么也没发生,也没有出现错误消息,而且没有添加任何行。

这是我的代码,我试图检查它,但似乎没有任何问题。

// send user data to a server side



let myUrl = NSURL(string: "http://thetogo.net/userRegister.php");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";

let postString = "email=\(userEmail)&password=\(userPassword)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);




        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) 
            data,response, error in

            if error != nil 
                print("error=\(error)")
                return
            

            do 

            let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

            if let parseJSON = json
                let resultValue = parseJSON["status"] as? String
                print("result: \(resultValue)")

                var isUserRegistered:Bool = false;
                if(resultValue=="Success")  isUserRegistered = true; 

                var messageToDisplay:String = parseJSON["message"] as! String!;
                if(!isUserRegistered)
                    messageToDisplay = parseJSON["message"] as! String!;
                

                dispatch_async(dispatch_get_main_queue(), 
                    //DisplayAlertMessage with confirmation
                    let myAlert = UIAlertController(title: "Alert", message:messageToDisplay, preferredStyle: UIAlertControllerStyle.Alert);

                    let okAction = UIAlertAction(title: "Ok", style:UIAlertActionStyle.Default) action in
                        self.dismissViewControllerAnimated(true, completion: nil);
                    
                    myAlert.addAction(okAction);
                    self.presentViewController(myAlert, animated:true, completion: nil);
                );



                 catch 
                    print(error)
            
         

这是我的php代码

<?php 


require("Conn.php");
require("MySQLDao.php");

$email = htmlentities($_POST["email"]);
$password = htmlentities($_POST["password"]);

$returnValue = array();

if(empty($email) || empty($password))

$returnValue["status"] = "error";
$returnValue["message"] = "Missing required field";
echo json_encode($returnValue);
return;


$dao = new MySQLDao();
$dao->openConnection();
$userDetails = $dao->getUserDetails($email);

if(!empty($userDetails))

$returnValue["status"] = "error";
$returnValue["message"] = "User already exists";
echo json_encode($returnValue);
return;


$secure_password = md5($password); // I do this, so that user password cannot be read even by me

$result = $dao->registerUser($email,$secure_password);

if($result)

$returnValue["status"] = "Success";
$returnValue["message"] = "User is registered";
echo json_encode($returnValue);
return;


$dao->closeConnection();

?>

【问题讨论】:

根据您发布的内容,我唯一能猜到的是它没有正确连接到您的数据库。而且由于那是在另一个类中,我看不到是否对该连接进行了错误检查,如果确实是原因,则会显示出来。 @pbush25 连接正常我在本地试过了,效果很好 那么我会拿出你的调试器,我会推荐一个名为 Charles 的工具,你可以用它来监控你的网络传输到你的网站,这样你就可以判断传输的内容是否是你所期望的,然后你也可以看到响应。 【参考方案1】:

1.) 在您的 php 代码中编写以下代码以检查错误:

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'on');

2.) 使用 $_GET http 方法,直接在浏览器上点击 url。

执行此操作后,您将看到您面临的错误。解决后 错误只是将 $_GET 替换为 $_POST

【讨论】:

以上是关于使用 POST 将用户信息存储在 Web 服务器 IOS 应用程序中的主要内容,如果未能解决你的问题,请参考以下文章

使用 POST 方法将值传递给 Web 服务 [关闭]

C ++ Qt - 仅在最后一个回复完成后发送POST请求

web访问时,用户信息和数据存储在哪里

C# 模拟 AD 用户

身份验证后存储附加信息[Spring]

php基础—— 表单和用户输入