Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jackie Jen
2,723 PointsHow android store value like php $_SESSION
Below is the scenario:
I have a login page of android that required to key in the username & password. Then those username & password value will pass to php login file. The php login file will then check through the database.
if the username and password is correct then will echo the user_id something like (123456) from the database.
Then the android part will get the 123456 user_id to said login successfully.
There are two problems i faced: i) how can i check in android part if the php file is echo out digit. if it is digit the login successfully else login failed
ii) Since android can retrieve the value from $_SESSION because $_SESSION is only for web server. Is it i need to store in share preference?
2 Answers
shezazr
8,275 PointsI am guessing that you just need to do a check i.e. on app in the login section, if i enter my email/password, you send that to server and see if it exists, if it exists, you indeed save it locally in the app..
Harry James
14,780 PointsThere are a few ways to go about this but, one of the best ways for what you're doing would probably be to use a method:
Class1.java
public class Class1 {
protected Class2 mClass2 = new Class2();
// Declare our second class here so we can access it in our code.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String username = "harryjamesuk";
String password= "password";
// Set the username and password variables.
mClass2.sendUsernameAndPass(username, password);
// Use the method to send the username and password variables across. You must declare the class member variable before the method!
}
}
Class2.java
class Class2 {
// I have removed the modifier from this class because, for security, you don't really want to make it protected or public.
protected String mUsername;
protected String mPassword;
// You can declare these early so that they are accessible throughout the entirety of the class (If you don't declare them here, due to scoping, the variables will only be accessible from the sendUsernameAndPass() method. Note that these start with a m for member (variables)!
protected void sendUsernameAndPass(String username, String password) {
// The variables username and password will be passed through to this method.
mUsername = username;
mPassword = password;
// Setting mUsername and mPassword's values now to what has been passed in.
someOtherMethod();
// Running the other method in this class, just to prove we can access these variables from outside this method as well.
}
protected void someOtherMethod() {
Log.d("Test", mUsername + " " + mPassword);
// This will log the username and password member variables to the console in the Debug view. Woohoo!
}
}
Hopefully this will make sense to you but, if there's anything you don't understand, give me a shout.
Alternative
You can also use an Intent if you are leading to another activity with this code:
Class1.java
public class Class1 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, Class2.class);
// Create the intent to go to Class2.
intent.putExtra("username", "harryjamesuk");
// Put an extra in - username is harryjamesuk.
intent.putExtra("password", "password");
// Put an extra in - password is password.
startActivity(intent);
// Start the intent!
}
}
Class2.java
public class Class2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.class2);
Bundle extras = getIntent().getExtras();
// Get our extras from our intent.
String username = extras.getString("username");
// Unbundle the username.
String password = extras.getString("password");
// Unbundle the password.
Log.d("Test", username + " " + password);
// Log our username and password to the console in Debug View.
}
Again, hopefully this will make sense to you but, if there's anything you don't understand, give me a shout.
Jackie Jen
2,723 PointsJackie Jen
2,723 PointsHi Shez,
what if android part received the user_id from php file, how can i check whether the user_id it received is digit or not. because in android part i only know how to check what string but not digit. below is the code i check the strings
result.equal("string")The reason i want to check as digits is because the user_id will be different if another user is login.