Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Android MySQL检查用户是否为管理员_Php_Android_Mysql_Android Volley - Fatal编程技术网

Php Android MySQL检查用户是否为管理员

Php Android MySQL检查用户是否为管理员,php,android,mysql,android-volley,Php,Android,Mysql,Android Volley,我有一个登录表单,用户将在其中输入用户名和密码 我想要实现的是: •检查用户是否为管理员、经理、普通用户 •然后我将显示一个toast/echo,表明该用户是管理员/经理/普通用户 这是我当前登录表单的源代码 import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android

我有一个登录表单,用户将在其中输入用户名和密码

我想要实现的是:

•检查用户是否为管理员、经理、普通用户

•然后我将显示一个toast/echo,表明该用户是管理员/经理/普通用户

这是我当前登录表单的源代码

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;


public class TwoFragment extends Fragment{
View view;
    Button reg;



    //Defining views
    private EditText editTextEmail;
    private EditText editTextPassword;
    private Button buttonLogin;


    //boolean variable to check user is logged in or not
    //initially it is false
    private boolean loggedIn = false;
    public TwoFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_two, container, false);
        reg = (Button) view.findViewById(R.id.button);
        buttonLogin = (Button) view.findViewById(R.id.btnsignin);
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });

        editTextEmail = (EditText) view.findViewById(R.id.txtemail);
        editTextPassword = (EditText) view.findViewById(R.id.editText);
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getActivity(), managerreg.class);
                startActivity(i);
            }
        });



        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        //In onresume fetching value from sharedpreference
        SharedPreferences sharedPreferences = getContext().getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);

        //Fetching the boolean value form sharedpreferences
        loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false);

        //If we will get true
        if(loggedIn){
            //We will start the Profile Activity
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
        }
    }

    private void login(){
        //Getting values from edit texts
        final String username = editTextEmail.getText().toString().trim();
        final String password = editTextPassword.getText().toString().trim();



        //Creating a string request
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.LOGIN_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //If we are getting success from server
                        if(response.equalsIgnoreCase(Config.LOGIN_SUCCESS)){
                            //Creating a shared preference
                            SharedPreferences sharedPreferences = getActivity().getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);

                            //Creating editor to store values to shared preferences
                            SharedPreferences.Editor editor = sharedPreferences.edit();

                            //Adding values to editor
                            editor.putBoolean(Config.LOGGEDIN_SHARED_PREF, true);
                            editor.putString(Config.USERNAME_SHARED_PREF,username );


                            //Saving values to editor
                            editor.commit();

                            //Starting profile activity
                            Intent intent = new Intent(getActivity(), MainActivity.class);
                            startActivity(intent);
                        }else{
                            //If the server response is not success
                            //Displaying an error message on toast
                            Toast.makeText(getActivity(), "Invalid username or password", Toast.LENGTH_LONG).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //You can handle error here if you want
                    }
                }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> params = new HashMap<>();
                //Adding parameters to request
                params.put(Config.KEY_USERNAME, username);
                params.put(Config.KEY_PASSWORD, password);


                //returning parameter
                return params;
            }
        };

        //Adding the string request to the queue
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
        requestQueue.add(stringRequest);
    }



}
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
导入com.android.volley.AuthFailureError;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.StringRequest;
导入com.android.volley.toolbox.volley;
导入java.util.HashMap;
导入java.util.Map;
公共类TwoFragment扩展了片段{
视图;
按钮注册;
//定义视图
私人编辑文本编辑电子邮件;
私人编辑文本编辑密码;
私人按钮按钮;
//用于检查用户是否登录的布尔变量
//起初这是错误的
私有布尔loggedIn=false;
公共双片段(){
//必需的空公共构造函数
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment_two,容器,假);
reg=(按钮)view.findViewById(R.id.Button);
buttonLogin=(Button)view.findViewById(R.id.btnsignin);
buttonLogin.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
登录();
}
});
editTextEmail=(EditText)view.findViewById(R.id.txtemail);
editTextPassword=(EditText)view.findViewById(R.id.EditText);
reg.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Intent i=新的Intent(getActivity(),managerreg.class);
星触觉(i);
}
});
返回视图;
}
@凌驾
恢复时公开作废(){
super.onResume();
//在onresume中从SharedReference获取值
SharedReferences SharedReferences=getContext().GetSharedReferences(Config.SHARED\u PREF\u NAME,Context.MODE\u PRIVATE);
//从SharedReferences获取布尔值
loggedIn=SharedReferences.getBoolean(Config.loggedIn\u SHARED\u PREF,false);
//如果我们能实现
if(loggedIn){
//我们将启动配置文件活动
Intent Intent=新的Intent(getActivity(),MainActivity.class);
星触觉(意向);
}
}
私有void登录(){
//从编辑文本中获取值
最终字符串username=editTextEmail.getText().toString().trim();
最终字符串密码=editTextPassword.getText().toString().trim();
//创建字符串请求
StringRequest StringRequest=新建StringRequest(Request.Method.POST,Config.LOGIN\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//如果我们从服务器获得成功
if(response.equalsIgnoreCase(Config.LOGIN\u SUCCESS)){
//创建共享首选项
SharedReferences SharedReferences=getActivity().GetSharedReferences(Config.SHARED\u PREF\u NAME,Context.MODE\u PRIVATE);
//创建编辑器以将值存储到共享首选项
SharedReferences.Editor=SharedReferences.edit();
//向编辑器添加值
putBoolean(Config.LOGGEDIN\u SHARED\u PREF,true);
putString(Config.USERNAME\u SHARED\u PREF,USERNAME);
//将值保存到编辑器
commit();
//启动配置文件活动
Intent Intent=新的Intent(getActivity(),MainActivity.class);
星触觉(意向);
}否则{
//如果服务器响应不成功
//在toast上显示错误消息
Toast.makeText(getActivity(),“无效用户名或密码”,Toast.LENGTH_LONG.show();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//如果需要,可以在此处处理错误
}
}){
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
//向请求添加参数
参数put(Config.KEY_用户名,用户名);
参数put(Config.KEY_PASSWORD,PASSWORD);
//返回参数
返回参数;
}
};
//将字符串请求添加到队列
RequestQueue RequestQueue=Volley.newRequestQueue(getActivity().getApplicationContext());
添加(stringRequest);
}
}
这是我当前的php:

<?php

    if($_SERVER['REQUEST_METHOD']=='POST'){
        $username = $_POST['username'];
        $password = $_POST['password'];




        require_once('dbConnect.php');

    $sql = "SELECT * FROM managerreg WHERE username = '$username' AND password = BINARY '".md5($_POST['password'])." ";

        $result = mysqli_query($con,$sql);

        $check = mysqli_fetch_array($result);

        if(isset($check)){
            echo 'success';
        }else{
            echo 'failure';
        }
    }