Android和PHP登录验证 [英] Android and PHP login authentication
问题描述
我试图让在用户需要登录的应用程序使用它在Android上的应用程序。登录认证将PHP Web服务来完成。我有一个 login.java
类 CustomeHTTPClient
而这一次是一个样本code我从网上找来的。有一个在 login.java
类名 connectphp
正在连接到Web服务,并使用响应显示敬酒消息的方法
Login.java
包com.boyzcorn.android.fyp;进口的java.util.ArrayList;
进口org.apache.http.NameValuePair;
进口org.apache.http.message.BasicNameValuePair;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;
公共类扩展登录活动{
/ **当第一次创建活动调用。 * /
的EditText ETEXT;
的EditText eText2;
按钮btnSubmit按钮;
按钮btnSignup; 公共无效验证()
{
如果(eText.getText()。的toString()。等于()||
eText.getText()。的toString()。等于())
{
Toast.makeText(getApplicationContext(),填充空字段,Toast.LENGTH_SHORT).show();
}
其他
{
connectphp();
}
}
公共无效connectphp()
{
// TODO自动生成方法存根
ArrayList的<&的NameValuePair GT; postParameters =新的ArrayList<&的NameValuePair GT;();
postParameters.add(新BasicNameValuePair(用户名,eText.getText()的toString()));
postParameters.add(新BasicNameValuePair(PASS1,eText2.getText()的toString())。);
传递参数以用于身份验证的PHP Web服务
//字符串有效=1;
串响应=无效;
尝试{
响应= CustomHttpClient.executeHttpPost(http://10.0.2.2:8082/WebService/login.php,postParameters); //输入您的远程PHP,ASP,Servlet的文件链接
字符串资源= response.toString();
//解析度= res.trim();
RES = res.replaceAll(\\\\ S +,);
//error.setText(res);
如果(res.equals(1))
{
Toast.makeText(getApplicationContext(),正确的用户名和密码,Toast.LENGTH_SHORT).show();
意图I =新意图(login.this,order_pushing.class);
startActivity(ⅰ);
}
其他
如果(res.equals(0))
{ Toast.makeText(getApplicationContext(),对不起!错误的用户名和密码,Toast.LENGTH_SHORT).show();
}
}赶上(例外五){ eText.setText(e.toString());
}}
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.login);
ETEXT =(EditText上)findViewById(R.id.uid);
eText2 =(EditText上)findViewById(R.id.editText2);
btnSubmit按钮=(按钮)findViewById(R.id.sbtn);
btnSignup =(按钮)findViewById(R.id.signupbtn);
btnSubmit.setOnClickListener(新OnClickListener(){公共无效的onClick(视图v)
{
验证(); (这是检查空字段)
}
});btnSignup.setOnClickListener(新View.OnClickListener(){ 公共无效的onClick(查看为arg0){
意图I =新意图(login.this,signup.class);
startActivity(ⅰ);
}
});}}
这是我的 customhtpclient
类中的HTTP连接定义。
** ** CustomHTTPClient.java包com.boyzcorn.android.fyp;进口的java.util.ArrayList;
进口org.apache.http.NameValuePair;
进口org.apache.http.message.BasicNameValuePair;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;
公共类扩展登录活动{
/ **当第一次创建活动调用。 * /
的EditText ETEXT;
的EditText eText2;
按钮btnSubmit按钮;
按钮btnSignup; 公共无效验证()
{
如果(eText.getText()。的toString()。等于()||
eText.getText()。的toString()。等于())
{
Toast.makeText(getApplicationContext(),填充空字段,Toast.LENGTH_SHORT).show();
}
其他
{
connectphp();
}
}
公共无效connectphp()
{
// TODO自动生成方法存根
ArrayList的<&的NameValuePair GT; postParameters =新的ArrayList<&的NameValuePair GT;();
postParameters.add(新BasicNameValuePair(用户名,eText.getText()的toString()));
postParameters.add(新BasicNameValuePair(PASS1,eText2.getText()的toString())。); //字符串有效=1;
串响应=无效;
尝试{
响应= CustomHttpClient.executeHttpPost(http://10.0.2.2:8082/WebService/login.php,postParameters); //输入您的远程PHP,ASP,Servlet的文件链接
字符串资源= response.toString();
//解析度= res.trim();
RES = res.replaceAll(\\\\ S +,);
//error.setText(res);
如果(res.equals(1))
{
Toast.makeText(getApplicationContext(),正确的用户名和密码,Toast.LENGTH_SHORT).show();
意图I =新意图(login.this,order_pushing.class);
startActivity(ⅰ);
}
其他
如果(res.equals(0))//如果服务器响应0
{ Toast.makeText(getApplicationContext(),对不起!错误的用户名和密码,Toast.LENGTH_SHORT).show();
}
}赶上(例外五){ eText.setText(e.toString());
}}
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.login);
ETEXT =(EditText上)findViewById(R.id.uid);
eText2 =(EditText上)findViewById(R.id.editText2);
btnSubmit按钮=(按钮)findViewById(R.id.sbtn);
btnSignup =(按钮)findViewById(R.id.signupbtn);
btnSubmit.setOnClickListener(新OnClickListener(){公共无效的onClick(视图v)
{
验证();
}
});btnSignup.setOnClickListener(新View.OnClickListener(){ 公共无效的onClick(查看为arg0){
意图I =新意图(login.this,signup.class);
startActivity(ⅰ);
}
});}}
和我的PHP Web服务名称 login.ph
< PHP
包括(config.php文件);
//用户名和密码的形式发送
接收参数
$名为myusername =和addslashes($ _ POST ['用户名']);
$ =输入mypassword和addslashes($ _ POST ['PASS1']);$ SQL =SELECT * FROM注册WHERE user_ID的='$名为myusername'和密码= $输入mypassword';$结果= mysql_query($的SQL);$行= mysql_fetch_array($结果);$主动= $行['主动'];$数= mysql_num_rows($结果);
如果结果匹配$名为myUsername,$输入mypassword,表行必须是1行
如果($计数== 1)
{回声1;(如果结果发现发送1至Android) }
其他
{
回声0;(如果结果没有发现Ø发送到Android) }?>
配置文件都连接参数,建立与数据库连接的是MySQL和我使用WAMP服务器。结果
*的的config.php 的*
< PHP$ mysql_hostname =localhost的;$ mysql_user =根;$ mysql_password =;$ mysql_database =KSE;$ BD = mysql_connect($ mysql_hostname,$ mysql_user,$ mysql_password)要么
死(哎呀有些事情出了问题);mysql_select_db($ mysql_database,$ BD)要么死(哎呀有些事情出了问题);
?>
当我运行我的应用程序它给我敬酒messgae如果我给错误日志中的细节,因为我用,如果与人在这里
如果(res.equals(1))
{
Toast.makeText(getApplicationContext(),正确的用户名和密码,Toast.LENGTH_SHORT).show();
意图I =新意图(login.this,order_pushing.class);
startActivity(ⅰ);
}
其他
如果(res.equals(0))
{ Toast.makeText(getApplicationContext(),对不起!错误的用户名和密码,Toast.LENGTH_SHORT).show();
}
块引用>但是,当我给正确的信息,没有response..Please帮助我。我只是一个初学者android.If你可以做我的code改正吧。
解决方案我忘了,从你WAMP服务器即开始我的web服务wamp- 2.and其次我删除此行$活跃= $行['主动'] ;从我的login.php和它现在所有工作
I am trying to make an app on android in which user needs to log in app to use it. log in authentication will be done by PHP web service. I have a
login.java
classCustomeHTTPClient
and this one is an sample code I have got from internet.There is a method inlogin.java
class nameconnectphp
is connecting to web service and using response to display toast messageLogin.java
package com.boyzcorn.android.fyp; import java.util.ArrayList; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class login extends Activity { /** Called when the activity is first created. */ EditText eText ; EditText eText2 ; Button btnSubmit ; Button btnSignup ; public void validation() { if(eText.getText().toString().equals("") || eText.getText().toString().equals("")) { Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show(); } else { connectphp(); } } public void connectphp() { // TODO Auto-generated method stub ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); postParameters.add(new BasicNameValuePair("username", eText.getText().toString())); postParameters.add(new BasicNameValuePair("pass1", eText2.getText().toString())); Passing Parameter to the php web service for authentication //String valid = "1"; String response = null; try { response = CustomHttpClient.executeHttpPost("http://10.0.2.2:8082/WebService/login.php", postParameters); //Enter Your remote PHP,ASP, Servlet file link String res=response.toString(); // res = res.trim(); res= res.replaceAll("\\s+",""); //error.setText(res); if(res.equals("1")) { Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show(); Intent i = new Intent(login.this,order_pushing.class); startActivity(i); } else if(res.equals("0")) { Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show(); } } catch (Exception e) { eText.setText(e.toString()); }} @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); eText = (EditText)findViewById(R.id.uid); eText2 = (EditText)findViewById(R.id.editText2); btnSubmit = (Button)findViewById(R.id.sbtn); btnSignup = (Button)findViewById(R.id.signupbtn); btnSubmit.setOnClickListener(new OnClickListener() { public void onClick(View v) { validation(); (This is to check empty fields) } }); btnSignup.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { Intent i = new Intent(login.this,signup.class); startActivity(i); } }); }}
This is my
customhtpclient
class in which http connection define.**CustomHTTPClient.java** package com.boyzcorn.android.fyp; import java.util.ArrayList; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class login extends Activity { /** Called when the activity is first created. */ EditText eText ; EditText eText2 ; Button btnSubmit ; Button btnSignup ; public void validation() { if(eText.getText().toString().equals("") || eText.getText().toString().equals("")) { Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show(); } else { connectphp(); } } public void connectphp() { // TODO Auto-generated method stub ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); postParameters.add(new BasicNameValuePair("username", eText.getText().toString())); postParameters.add(new BasicNameValuePair("pass1", eText2.getText().toString())); //String valid = "1"; String response = null; try { response = CustomHttpClient.executeHttpPost("http://10.0.2.2:8082/WebService/login.php",postParameters); //Enter Your remote PHP,ASP, Servlet file link String res=response.toString(); // res = res.trim(); res= res.replaceAll("\\s+",""); //error.setText(res); if(res.equals("1")) { Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show(); Intent i = new Intent(login.this,order_pushing.class); startActivity(i); } else if(res.equals("0"))//Server response if 0 { Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show(); } } catch (Exception e) { eText.setText(e.toString()); }} @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); eText = (EditText)findViewById(R.id.uid); eText2 = (EditText)findViewById(R.id.editText2); btnSubmit = (Button)findViewById(R.id.sbtn); btnSignup = (Button)findViewById(R.id.signupbtn); btnSubmit.setOnClickListener(new OnClickListener() { public void onClick(View v) { validation(); } }); btnSignup.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { Intent i = new Intent(login.this,signup.class); startActivity(i); } }); }}
And my php web service name login.ph
<?php include("Config.php"); // username and password sent from Form
Receiving parameters $myusername=addslashes($_POST['username']);
$mypassword=addslashes($_POST['pass1']); $sql="SELECT * FROM signup WHERE user_id='$myusername' and password='$mypassword'"; $result=mysql_query($sql); $row=mysql_fetch_array($result); $active=$row['active']; $count=mysql_num_rows($result);
If result matched $myusername and $mypassword, table row must be 1 row
if($count==1) { echo "1";(If result found send 1 to android) } else { echo "0";(If result not found send o to android) } ?>
Config file have all connection parameters to establish connection with database that is mysql and I am using wamp server.
*Config.php*<?php $mysql_hostname = "localhost"; $mysql_user = "root"; $mysql_password = ""; $mysql_database = "kse"; $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong"); mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong"); ?>
When I am running my app it is giving me toast messgae if I'm giving wrong log in details as I use if and else here
if(res.equals("1")) { Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show(); Intent i = new Intent(login.this,order_pushing.class); startActivity(i); } else if(res.equals("0")) { Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show(); }
But when I am giving correct information, no response..Please help me .I am just a beginner to android.If you could make corrections in my code please.
解决方案I forgot to start my web service from thee wamp server i.e wamp- 2.and secondly i delete this line "$active=$row['active'];" from my login.php and its all working now
这篇关于Android和PHP登录验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!