• mwen82@gmail.com

    Thank you for the very helpful tutorial. May I ask how to link the user interface with the php scripts?

    You’ve given me useful php code but I’m not sure how to use that to authenticate the user session with the wp_users table at program launch, or after email and password submission at the login page. I built the user interface already.

    Should I use NSURL to pass the email and password to the $wpdb? How would I do that with this app?

    I am new to programming. Thank you greatly for any help!

    • Thank you for your questions. It is difficult to answer your questions here in the comments box and if I do it might not be as clear as a video. Unfortunately I do not have more videos on Integrating your app with WordPress site but if you are willing to consider using custom database other than wordpress existing database and users table then this course of mine can probably help you. Have a look at the introduction and the video explaining what the course is about. I created a discount coupon for you to take this course for $9 only. If you are interested.
      Here is a link to my video course: https://www.udemy.com/swift-php-mysql-user-sign-up-sign-in-sign-out-and-more/?couponCode=9_dollars

      • mwen82@gmail.com

        Do you do custom projects? I am developing a simple order placement app for an existing customer database and must use their existing logins. Would you be willing to consult by the hour for any help regarding this? Your tut is helpful and almost exactly what I was looking for.

        I imagine it must have something to do with NSURLsession, but how do you apply here?

        Here is a code snippet I took from https://dipinkrishna.com/blog/2014/07/login-signup-screen-tutorial-xcode-6-swift-ios-8-json/


        import UIKit

        class LoginVC: UIViewController,UITextFieldDelegate {

        @IBOutlet var txtUsername : UITextField!
        @IBOutlet var txtPassword : UITextField!

        override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        }

        override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        }

        /*
        // #pragma mark - Navigation

        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
        }
        */

        @IBAction func signinTapped(sender : UIButton) {
        let username:NSString = txtUsername.text!
        let password:NSString = txtPassword.text!

        if ( username.isEqualToString("") || password.isEqualToString("") ) {

        let alertView:UIAlertView = UIAlertView()
        alertView.title = "Sign in Failed!"
        alertView.message = "Please enter Username and Password"
        alertView.delegate = self
        alertView.addButtonWithTitle("OK")
        alertView.show()
        } else {

        do {
        let post:NSString = "username=(username)&password=(password)"

        NSLog("PostData: %@",post);

        let url:NSURL = NSURL(string:"https://localhost/loginform2.php")!

        let postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!

        let postLength:NSString = String( postData.length )

        let request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        request.HTTPBody = postData
        request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.setValue("application/json", forHTTPHeaderField: "Accept")

        var reponseError: NSError?
        var response: NSURLResponse?

        var urlData: NSData?
        do {
        urlData = try NSURLConnection.sendSynchronousRequest(request, returningResponse:&response)
        } catch let error as NSError {
        reponseError = error
        urlData = nil
        }

        • Please help me understand your problem first.
          1. Are you trying to send HTTP Post request and it does not reach server side?

          2. Are you trying to run PHP script to perform user login with WordPress and it does not work

          3. Are you trying to navigate to a different view controller after you received a response from php script but the app does not take you to a different view?

          • mwen82@gmail.com

            Thank you.

            I’m trying to send HTTP Post request with the following PHP script:

            header('Content-type: application/json');

            if($_POST) {

            $username = $_POST['username'];

            $password = $_POST['password'];

            if($username && $password) {

            $db_name = 'database';

            $db_user = 'user';

            $db_password = 'pass';

            $server_url = 'localhost';

            $mysqli = new mysqli('localhost', $db_user, $db_password, $db_name);

            /* check connection */

            if (mysqli_connect_errno()) {

            error_log("Connect failed: " . mysqli_connect_error());

            echo '{"success":0,"error_message":"' . mysqli_connect_error() . '"}';

            } else {

            if ($stmt = $mysqli->prepare("SELECT username FROM users WHERE username = ? and password = ?")) {

            $password = md5($password);

            /* bind parameters for markers */

            $stmt->bind_param("ss", $username, $password);

            /* execute query */

            $stmt->execute();

            /* bind result variables */

            $stmt->bind_result($id);

            /* fetch value */

            $stmt->fetch();

            /* close statement */

            $stmt->close();

            }

            /* close connection */

            $mysqli->close();

            if ($id) {

            error_log("User $username: password match.");

            echo '{"success":1}';

            } else {

            error_log("User $username: password doesn't match.");

            echo '{"success":0,"error_message":"Invalid Username/Password"}';

            }

            }

            } else {

            echo '{"success":0,"error_message":"Invalid Username/Password."}';

            }

            }else {

            echo '{"success":0,"error_message":"Invalid Data."}';

            }

            ?>

            As you can see it’s not compatible with WordPress db. WPDB doesn’t use md5 hash either. Any ideas?

  • Jude

    Does this work with wordpress 4.7? I get a HTTP 500 when trying to call the url

  • Appels+Oranjes

    Something is missing here: how do I integrate that PHP into WP? That is: if I use that code from outside WP (simply requesting the PHP page), I suppose it won’t work because there are custom WP functions inside. So, how to integrate that code?