不能身份验证 [英] Cannot authentication

查看:90
本文介绍了不能身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个时候,我想用户输入他/她的用户名和密码(数据嘲笑@MySQL)
但是,这是行不通的。

At this time, I want user to enter his/her username and password(Data is mocked @MySQL) But it does not work.

我的f​​ile.php:

My file.php :

<?php
    include("ConnectDatabase.php");
    $Username = $_POST['Username'];
    $Password = $_POST['Password'];

    $q = mysql_query("SELECT Username, Password FROM Users
                    where Username = '".$Username."' and
                    Password = '".$Password."'");

    if(mysql_num_rows($q) > 0){
        print json_encode($q);
    }else{
        print "1";
    }
     mysql_close();
   ?>

我的Java文件:

My java file :

public class Authentication extends Activity implements OnClickListener,
        OnKeyListener, OnCheckedChangeListener {

    /** Called when the activity is first created. */

    ArrayList<NameValuePair> authentication;
    String passIn, userIn, result;
    EditText username, password;
    CheckBox remember;
    Button b_login;
    InputMethodManager inputManager;
    InputStream is;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // userIn = username.getText().toString();
        // passIn = password.getText().toString();
        username = (EditText) findViewById(R.id.usrname);
        password = (EditText) findViewById(R.id.password);
        inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        remember = (CheckBox) findViewById(R.id.remember);
        remember.setOnCheckedChangeListener(this);
        b_login = (Button) findViewById(R.id.login);
        b_login.setOnClickListener(this);
        username = (EditText) findViewById(R.id.usrname);
        username.setOnKeyListener(this);
        password = (EditText) findViewById(R.id.password);
        password.setOnKeyListener(this);

    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.login:
            while (true) {
                userIn = username.getText().toString();
                passIn = password.getText().toString();
                try {
                    sendAuthenticationData(userIn, passIn);
                    break;
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (WrongInputException e) {
                    Toast.makeText(Authentication.this, "Invalid username or password", Toast.LENGTH_LONG);

                }
//              break;
            }
            Intent intent = new Intent(this, ApplicationMenus.class);
            this.startActivity(intent);
            clearText(username,password);

            break;
        }
    }

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if ((event.getAction() == KeyEvent.ACTION_DOWN)
                && (keyCode == KeyEvent.KEYCODE_ENTER)) {
            // Perform action on key press

            inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
            return true;
        }
        return false;
    }


    public void clearText(EditText usr, EditText pass) {
        usr.setText("");
        pass.setText("");
    }

    public void sendAuthenticationData(String username, String password)
            throws ClientProtocolException, IOException, WrongInputException {

        authentication = new ArrayList<NameValuePair>();
        authentication.add(new BasicNameValuePair("Username", userIn));
        authentication.add(new BasicNameValuePair("Password", passIn));
        this.sendData(authentication);
    }

    public void sendData(ArrayList<NameValuePair> data)
            throws ClientProtocolException, IOException, WrongInputException {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "path/Authentication.php"); // I use real path here
        httppost.setEntity(new UrlEncodedFormEntity(data));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
//      is = entity.getContent();
        String temp = EntityUtils.toString(entity);

        if (temp.equals("1")) {
            throw new WrongInputException();
        }

    }}

这是我第一次写code与PHP服务器和JSON连接。我pretty使用JSON迷惑了很多,虽然我试图按照许多样品。鸭preciate任何帮助。

This is my first time to write code that connect with PHP server, and JSON. I pretty confuse with JSON a lot, though I tried to follow many samples. Appreciate for any help

推荐答案

您忘记的第一件事就是 mysql_real_escape_string 为传入变量:

The first thing you forgot is mysql_real_escape_string for the incoming variables:

$Username = $_POST['Username'];

和您的第二个问题是可能在:

And your second problem is likely this:

   if (mysql_num_rows($q) > 0) {
        print json_encode($q);

$ Q 变量是一个mysql_结果的处理。它不能是JSON-RE presented。你可能想用:

The $q variable is a mysql_ result handle. It cannot be JSON-represented. You probably wanted to use:

        print json_encode(mysql_fetch_assoc($q));

所以,你会得到至少一个结果数组/使用用户名/密码有对象。也许它会更容易,如果你只是发送另一个简单的数字结果返回 - 打印2; 什么

也有,如果你运行PHP 5.2或更高版本检查,以致于 json_en $ C $ç 肯定是可用?

Also have you checked if you run PHP 5.2 or later, so that json_encode is certainly available?

这篇关于不能身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆