json - Throwing out same response on login check PHP AND SWIFT -
i trying try , learn swift want backend server php , need convert php json app
now want put email , password in fields on app , send post request php server side , php validates details , sends response let me decide response.
i have managed post server side success think when response in xcode gives same output weather details correct or in-correct,i have looked every , followed tutorials keep getting same problem i'm guessing way check details in php file error , not success.
could help?
login.php
$email = addslashes(strip_tags($_post['email'])); $password = addslashes(strip_tags($_post['password'])); $password = md5($password); $returnvalue = array(); $sql = "select email, user_password `accounts` `email` = '$email' , user_password = '$password' limit 1"; $fetchuser = mysqli_query($db_connect, $sql); $row = mysqli_num_rows($fetchuser); if($row = mysqli_num_rows($fetchuser) == 0){ $returnvalue["status"] = "error"; $returnvalue["message"] = "account not found"; echo json_encode($returnvalue); } else { $returnvalue["status"] = "success"; $returnvalue["message"] = "account found"; echo json_encode($returnvalue); }
swift code
let useremail = useremailtextfield.text; let userpassword = userpasswordtextfield.text; if(useremail!.isempty || userpassword!.isempty) { return; } //send server let request = nsmutableurlrequest(url: nsurl(string: "http://chaozsounds.com/chaozsounds/new-chaozsounds/teamchaozapp/register.php")!) request.httpmethod = "post" let poststring = "email=\(useremail)&password=\(userpassword)" request.httpbody = poststring.datausingencoding(nsutf8stringencoding) let task = nsurlsession.sharedsession().datataskwithrequest(request) { data, response, error in guard error == nil && data != nil else { // check fundamental networking error print("error=\(error)") return } if let httpstatus = response as? nshttpurlresponse httpstatus.statuscode != 200 { // check http errors print("statuscode should 200, \(httpstatus.statuscode)") print("response = \(response!)") } let returnvalue = nsstring(data: data!, encoding: nsutf8stringencoding) print("returnvalue = \(returnvalue!)") if(returnvalue == "success") { // login successful nsuserdefaults.standarduserdefaults().setbool(true, forkey:"isuserloggedin"); nsuserdefaults.standarduserdefaults().synchronize(); self.dismissviewcontrolleranimated(true, completion: nil); } } task.resume()
ok had work around , managed after put code below
// send post request let request = nsmutableurlrequest(url: nsurl(string: "urllinkhere")!); request.httpmethod = "post"; let poststring = "email=\(useremail!)&password=\(userpassword!)"; request.httpbody = poststring.datausingencoding(nsutf8stringencoding); // success or error let task = nsurlsession.sharedsession().datataskwithrequest(request) { data, response, error in if error != nil { print("no data found: \(error)") return } if let httpstatus = response as? nshttpurlresponse httpstatus.statuscode != 200 { // check http errors print("statuscode should 200, \(httpstatus.statuscode)") print("response = \(response!)") } // this, on other hand, can quite fail if there's server error, // want wrap in `do`-`try`-`catch`: { let json = try nsjsonserialization.jsonobjectwithdata(data!, options: .mutablecontainers) as? nsdictionary if let parsejson = json { let returnvalue = parsejson["status"] as? string; print("returnvalue = \(returnvalue!)") if(returnvalue == "success") { // success nsuserdefaults.standarduserdefaults().setbool(true,forkey:"isuserloggedin"); nsuserdefaults.standarduserdefaults().synchronize(); self.dismissviewcontrolleranimated(true, completion: nil); } } } catch { print(error) } } task.resume()
Comments
Post a Comment