我正在尝试在android中执行会话处理过程.
在这里,我已经通过androID成功登录,现在我想要处理登录用户的会话.
这是我的login_suer.java(androID部分)
package com.iwantnew.www;import java.util.ArrayList;import java.util.List;import org.apache.http.nameValuePair;import org.apache.http.message.BasicnameValuePair;import org.Json.JsONException;import org.Json.JsONObject;import androID.app.Activity;import androID.app.ProgressDialog;import androID.content.Intent;import androID.os.AsyncTask;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.TextVIEw;public class login_user extends Activity{ private ProgressDialog pDialog; JsONParser JsonParser = new JsONParser(); EditText login_email; EditText login_password; button signin; TextVIEw error_msg; private static String url_create_signin= "http://10.0.2.2/androID_iwant/login_user.PHP"; // JsON Node names private static final String TAG_SUCCESS = "success"; public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.user_form); // Edit Text login_email = (EditText) findVIEwByID(R.ID.login_email); login_password = (EditText) findVIEwByID(R.ID.login_password); signin = (button) findVIEwByID(R.ID.signin); error_msg = (TextVIEw) findVIEwByID(R.ID.error_msg); signin.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { // creating new product in background thread new CheckLogin().execute(); } }); } class CheckLogin extends AsyncTask<String, String, String> { /** * Before starting background thread Show Progress Dialog * */ @OverrIDe protected voID onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(login_user.this); pDialog.setMessage("Signing in.."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } /** * Creating product * */ protected String doInBackground(String... args) { //Building Parameters List<nameValuePair> params = new ArrayList<nameValuePair>(); params.add(new BasicnameValuePair("email",login_email.getText().toString())); params.add(new BasicnameValuePair("password", login_password.getText().toString())); // getting JsON Object // Note that create product url accepts POST method JsONObject Json = JsonParser.makehttpRequest(url_create_signin, "POST", params); // check log cat fro response Log.d("Create Response", Json.toString()); // check for success tag try { int success = Json.getInt(TAG_SUCCESS); if (success == 1) { // successfully created users Intent i = new Intent(getApplicationContext(), post_item.class); startActivity(i); // closing this screen finish(); } else { // Failed to sign in error_msg.setText("Incorrect username/password"); } } catch (JsONException e) { e.printstacktrace(); } return null; } /** * After completing background task dismiss the progress dialog * **/ protected voID onPostExecute(String file_url) { // dismiss the dialog once done pDialog.dismiss(); } }}@H_301_9@
现在我需要在这个java文件中启动会话处理的想法.
服务器端的代码如下:即login_user.PHP
<?PHPsession_start();// array for JsON response$response = array();if(isset($_POST['email']) && isset($_POST['password'])){ $email = $_POST['email']; $password = $_POST['password'];// include db handler require_once 'DB_Functions.PHP'; $db = new DB_Functions(); $user = $db->getUesrByEmailAndPassword($email, $password); if ($user != false) { // user found // echo Json with success = 1 $response["success"] = 1; $response["uID"] = $user["unique_ID"]; $response["user"]["name"] = $user["name"]; $response["user"]["email"] = $user["email"]; $response["user"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; echo Json_encode($response); } else { // user not found // echo Json with error = 1 $response["error"] = 1; $response["error_msg"] = "Incorrect email or password!"; echo Json_encode($response); }}?>@H_301_9@
上面这个PHP文件中使用的函数是getUesrByEmailAndPassword($email,$password)
在下面 :
public function getUserByEmailAndPassword($email, $password) { $result = MysqL_query("SELECT * FROM users WHERE email = '$email'") or dIE(MysqL_error()); // check for result $no_of_rows = MysqL_num_rows($result); if ($no_of_rows > 0) { $result = MysqL_fetch_array($result); $salt = $result['salt']; $encrypted_password = $result['encrypted_password']; $hash = $this->checkhashSSHA($salt, $password); // check for password equality if ($encrypted_password == $hash) { // user authentication details are correct //return $result; session_start(); $_SESSION['clIEntID'] = $result[0]; $_SESSION['logged_in'] = TRUE; } } else { // user not found return false; } }@H_301_9@
请帮助我使我的代码工作.
任何帮助将不胜感激.
包含此类问题解决方案的任何链接对我都有帮助.谢谢!
解决方法:
据我所知,你的getUserByEmailAndPassword()在成功检查密码后永远不会返回实际的用户数据. //返回$result;被注释掉了. $user因此为null,客户端收到“电子邮件或密码不正确!”信息.
另一件事.要使PHP会话正常工作,客户端必须接收并记住其session_ID,并将其作为GET或cookie参数发送给每个请求.看看你的代码,我没有看到androID收到它的session_ID.见:http://www.php.net/manual/en/session.idpassing.php
顺便说一下,直接在POST中使用未经校对的$email进行SQL查询是一个坏主意.见:How can I prevent SQL injection in PHP?
总结以上是内存溢出为你收集整理的java – 在php中登录服务器端时在android中进行会话处理全部内容,希望文章能够帮你解决java – 在php中登录服务器端时在android中进行会话处理所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)