【问题标题】:Server side PHP session is not working in android服务器端 PHP 会话在 android 中不起作用
【发布时间】:2015-08-23 18:38:35
【问题描述】:

我在android中使用登录概念,登录一个用户名一次登录,只有相同的用户名不能第二次登录。它工作正常。现在我想在服务器端使用会话超时概念,但它不起作用,值正在存储到会话中,但它无法获取另一个 php 文件。

我的登录php代码

session_start();                
$_SESSION['user'] =1; 

第二个php代码

session_start();
    if(isset($_SESSION['user']))
    {
    //my action
    }

我的安卓代码

  DefaultHttpClient httpClient = new DefaultHttpClient();
            httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " +
                    "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
                httpPost.setHeader("Accept", "text/html,application/xml," +
                    "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
                httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

【问题讨论】:

标签: php android session session-cookies


【解决方案1】:

这是您用于第一个和第二个请求的代码吗?

如果是这样,问题是您总是创建一个新客户端,这会在服务器端产生新会话,因为存储在 cookie 中的 sessionid 不可用。

所以创建你的 httpClient 一次,并为两个请求使用相同的实例。应该能解决问题

像这样:

 DefaultHttpClient httpClient = new DefaultHttpClient();
            httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);

            HttpPost httpPost1 = new HttpPost(url1);

        httpPost1.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " +
                "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
            httpPost1.setHeader("Accept", "text/html,application/xml," +
                "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
            httpPost1.setHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost1.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost1);
        HttpEntity httpEntity = httpResponse.getEntity();
        is1 = httpEntity.getContent();


        HttpPost httpPost2 = new HttpPost(url2);
        httpPost2.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " +
                "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
            httpPost2.setHeader("Accept", "text/html,application/xml," +
                "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
            httpPost2.setHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost2.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse2 = httpClient.execute(httpPost2);
        HttpEntity httpEntity2 = httpResponse2.getEntity();
        is2 = httpEntity2.getContent();

【讨论】:

  • 添加示例回答
猜你喜欢
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
  • 1970-01-01
  • 2021-08-17
  • 2013-09-08
  • 2014-09-28
  • 1970-01-01
相关资源
最近更新 更多