• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps login page user details fetched from database

krishnaveni

Well-Known Member
Am developed one login form...here post the username and password from database using webservices...now its redirect to another activity,...that another activity having firstname and lastname for particular loged person details only fetched from database using json web services...
Because more number of users are there in admin side(ajay,krish,hari).... i need who r loged(ajay) in the login page that user(ajay) details only displayed
 
You have to set in where the name of the person logged in.

$q=mysql_query("SELECT firstname,lastname FROM customers WHERE firstname =(name_of_user_logged)");
 
thanks for ur rly...already i set it...but it is not valuable coding....because ajay logged means ajay name only displayed...but i need all admin users logged in login page...its permission is give...which user loged means that particular user firstname and lastname only displayed...dis is my doubt...

here i am finished some coding part...
LoginLayoutActivity.java
package com.example.login;

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.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class LoginLayoutActivity extends Activity {

EditText un,pw;
TextView error;
Button ok;
protected static String username;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
un=(EditText)findViewById(R.id.et_un);
pw=(EditText)findViewById(R.id.et_pw);
ok=(Button)findViewById(R.id.btn_login);
error=(TextView)findViewById(R.id.tv_error);

ok.setOnClickListener(new View.OnClickListener() {



@Override
public void onClick(View v) {
// TODO Auto-generated method stub

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));

String response = null;
try {

response = CustomHttpClient.executeHttpPost("xxx/login3.php", postParameters);
String res=response.toString();
res= res.replaceAll("\\s+","");

if(res.equals("1"))
{

error.setText("Correct Username or Password");
Intent i = new Intent(getApplicationContext(),registerActivity.class);
startActivity(i);


}
else
{
error.setText("Sorry!! Incorrect Username or Password");
}
} catch (Exception e) {
un.setText(e.toString());
}

}
});
}
}

registerActivity.java:
package com.example.login;

import java.io.BufferedReader;



import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;


public class registerActivity extends Activity {
/** Called when the activity is first created. */

TextView txt;
JSONObject json;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a crude view - this should really be set via the layout resources
// but since its an example saves declaring them in the XML.
LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);

// Set the text and call the connect function.
txt.setText("Connecting...");
//call the method to run the data retreival
txt.setText(getServerData(KEY_121));



}
public static final String KEY_121 = "xxx/lo.php"; //i use my real ip here



private String getServerData(String returnString) {

InputStream is = null;

String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("usertype","A"));

//http post
try{


HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}

//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
Log.i("log_tag","Line reads: " + line);

}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);



for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag",
"firstname: "+json_data.getString("firstname")+
"lastname: "+json_data.getString("lastname")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);

}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}

}

login3.php:

<?php
// store session data
$un=$_POST['login'];
$pw=$_POST['password'];
//connect to the db
$user = 'root';
$pswd = '';
$db = 'xxx';
$conn = mysql_connect('localhost', $user, $pswd);
mysql_select_db($db, $conn);
//run the query to search for the username and password the match
$query = "SELECT firstname,lastname FROM customers WHERE login = '$un' AND password = '$pw' OR usertype ='A'";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
//this is where the actual verification happens
if(mysql_num_rows($result) > 0)
{

echo 1;
}
else
{
echo 0;

}
?>

lo.php:

<?php


$r=mysql_connect("localhost","root","");
mysql_select_db("xxx",$r);

$q=mysql_query("SELECT firstname,lastname FROM xcart_customers WHERE (usertype ='A')");

while($e=mysql_fetch_assoc($q))
{
$row[]=$e;
}
print(json_encode($row));



mysql_close()
?>

here lo.php am set usertype=A ...bez all admin user r logged in login page...that means admin users having permission... so here all admin user firstname and lastname displayed...but i need which admin person loged in login page that particular person detail only displayed...so plz give me some solutions...(i.e)krishnaveni,kannan,hariprasad r admin user means when krishnaveni loged means krishnaveni detail ly displayed...kannan logged means kannan details only displayed..i dono how it is do???? plz give me solutions....thanks in advance...
 
I understand, I think you could create a table of logged admins, or add a field in the user table with a flag Logged.

I don't know your structure, the best way to control sessions is with objects scope, if you could add same object in the server that is destroyed when the user log-off, you could search for the objects in the server to know who is logged.
 
i used dis coding in mu LoginLayoutActivity.java:
HttpSession session = request.getSession(true);
HttpSession session = request.getSession (true);
if (session.isNew() == false) {
session.invalidate();
session = request.getSession(true);
}
session.setAttribute("username", firstname);
firstname username= (firstname )
session.getAttribute('username
');
if (username
== null) {
response.sendRedirect
("
LoginLayoutActivity");
return;
}

but i got error....invalid character constant in that pink color username..now how it is cleared...or any other solutions means give me...
 
Back
Top Bottom