0

嗨,我对此感到厌烦和厌倦,无法在网上找到任何解决方案,所以这是我的问题。我正在使用 Apache HTTP 帖子登录网站。提供我的凭据。但是无论我从网络上使用什么代码片段,无论我做什么,它总是会再次返回我的登录页面作为响应。这是我的代码,请提供解决方案。

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", "xxx"));
            nameValuePairs.add(new BasicNameValuePair("password", "xxx"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);

            Log.i("Response num:", "" + response.getStatusLine().toString());

            Log.i("response page:", "" + responseHandler(response));

            Log.i("Headers:", "" + response.getHeaders("Location").toString());

        } catch (ClientProtocolException e) {

        } catch (IOException e) {

        }

php脚本是:

<?php  

session_start();

include('Connectdb.php');



$result = mysql_query("SELECT * FROM user_accounts where username ='".$_POST["username"]."' AND password ='".$_POST["password"]."'");



if($row = mysql_fetch_array($result))

  {

   $_SESSION['Suserid']=$row['hospital_id'];

 // mysql_close($con);

  if ($row['acc_type']=="patient"){
$_SESSION['verifiedUserType']="patient";



 header('Location:./home_P.php');
 }

 else

 if ($row['acc_type']=="admin"){

     $_SESSION['verifiedUserType']="admin";

 header('Location:./home_admin.php');}
 else
 if ($row['acc_type']=="doctor"){

     $_SESSION['verifiedUserType']="doctor";

 header('Location:./home_doc.php');}

 exit;

  }

else

{



mysql_close($con);



//session_start();


 header('Location:./index.php?failed=1');





}

?>

如何在登录后获取页面并在用户登录后通过获取 cookie 来维护会话?ps 即使我输入错误的用户名或密码,我也会得到 http 1.1 200 OK 状态。

4

3 回答 3

1

您需要在应用程序中实现响应处理。这是我的第一个应用程序的登录凭据片段,它与您的非常相似,并且对我有用。

    case R.id.login_login_but:
        Toast.makeText(this, "Logging in...", Toast.LENGTH_SHORT).show();

        ArrayList<NameValuePair> postLogin = new ArrayList<NameValuePair>();
        postLogin.add(new BasicNameValuePair("post_user", "User"));
        postLogin.add(new BasicNameValuePair("post_pass", "Pass));

        try {
            String response = null;
            response = CustomHttpClient.executeHttpPost(
                    "http://giveaway.synamegames.com/appfiles/login.php", postLogin);
            String res = response.toString();
            res = res.replaceAll("\\s+", "");

            if (res.equals(1)) {
            // logged in
            } else {
            // incorrect user or password
            }

        } catch (Exception e) {
            Toast.makeText(this, "Server timeout please try again later. You must have internet.", Toast.LENGTH_SHORT).show();
        }
    break;

而我使用的 PHP 脚本是...

<?php
$username=$_POST['post_user'];
$password=$_POST['post_pass'];
$user = 'db_user';
$pswd = 'db_password';
$db = 'db_name';
$server = 'www.domain.com';
$conn = mysql_connect($server, $user, $pswd);
mysql_select_db($db, $conn);
$query=mysql_query("SELECT * FROM users  WHERE  pass =('$password') AND user = $username")or die(mysql_error());

if(mysql_num_rows($query)==1) {
echo 1;
} else {
echo 0;
}
mysql_close($conn);
?>

PS。如果它有效,请标记为正确:)

于 2012-10-16T07:41:18.480 回答
0

建立连接后,设置一些条件来检查名称和密码是否为 null 。

if(users != null && password != null){
            try{
                // send the name and password along with the url
                                // and authenticate the values.

            }catch (JSONException e) {
                e.printStackTrace();
            }
        }

如果已经登录,那么下次此条件将自动验证并打开第二页..

于 2012-10-16T07:32:48.853 回答
0

您可以从您的登录请求中获取 cookie 列表,如下所示:

List<Cookie> cookies = httpclient.getCookieStore().getCookies();

并使用请求发送正确的 cookie:

CookieStore store = new BasicCookieStore();
store.addCookie(yourCookie);
httpclient.setCookieStore(store);

我建议您使用 md5 将您的密码存储在数据库中。您永远不应该以明文形式保存密码。也许它是 md5,这就是它不起作用的原因?

于 2012-10-16T07:38:44.763 回答