0

i'm expert in php , mysql programming... i write a php,mysql script that users can login, register, have profiles and so much more... i'm just a beginner in android programming... i write a android app that have a webview for show mobile version of my site..

i want know: how can i check the user logged / or not (sessions created with php when user login is success)? (really i want check for example $_SESSION['logged'] in my android app for show a specific activity (if session exsits, show activity1 , and if not exit show activity2)

important note : i'm a bigginer in android!

sorry for my English (English, is not my native language!)

my problem solved! You can use this tutorial ...... that is very very easy and understandable ... Android Login and Registration with PHP, MySQL and SQLite

Mojtaba GS3
  • 51
  • 1
  • 12

1 Answers1

0

I am not a professional. Hope I can solve your problem. I have two method to do the member system.

First, I will create a Data model like User.java

-> User.java

class User{
 private String username = "";
 private String password = "";

// Getter and Setter..
}

while a user is login, create a new user object.

User user = new User();
user.setPassword(...);
user.setUsername(...);

When you need to get information from server by httprequest. What you need to do is get the information from User object

 Url url = new Url("http://your.server/index.php?username=" + user.getUsername() + "&password=" + user.getPassword());
 HttpURLRequest conn = url.openConnection();
 .......

Further information. I usually save the cookie from the login process.

ArrayList<String> cookiesList = new ArrayList<String>();
Map<String, List<String>> mHeaderMap = mConnection.getHeaderFields();
String headername = "";
for(int i = 0; (headername = mConnection.getHeaderFieldKey(i)) != null; i++){
    if(headername.equals("Set-Cookie")){
        cookiesList.add(mConnection.getHeaderField(i));
    }
}

Then if you send another login request. you use the cookie again and send to the server.

mConnection.setRequestProperty("Cookie", cookie);

More Information here.

On server side.

<?php
session_start();
if($_GET.count() > 0)
$username = $_GET['username'];
if($_GET.count() > 0)
$password = $_GET['password'];
$arr = array();

if($_SESSION['username'] == $username && $_SESSION['password'] == password){
 $arr = array("isValid" => true, "message" => "");
}else{
 $arr = array("isValid" => false, "message" => "");
}
echo json_encode($arr);
?>

On android

if(isValid){
  startActicity(....)
}
Jeff Lee
  • 783
  • 9
  • 17
  • your answer help me but not completely... tnx alot but i want read seassion ... – Mojtaba GS3 May 20 '13 at 06:58
  • You should do it in server side I think. Actually the cookie you catch on client side is referring to the session on server side. – Jeff Lee May 20 '13 at 07:05
  • [More information about Cookie and Session](http://en.wikipedia.org/wiki/HTTP_cookie#Session_cookie) Hope I can help you. :D – Jeff Lee May 20 '13 at 07:08
  • For android, it would be suitable to use Shared Preferences for User Settings instead of putting it on a Map because Map uses Heap memory and after closing the app, values saved into it will also be gone. – vincent May 20 '13 at 08:47