gpt4 book ai didi

php - 服务器端 PHP session 在 android 中不起作用

转载 作者:行者123 更新时间:2023-11-30 02:05:22 25 4
gpt4 key购买 nike

我在 android 中使用登录概念,登录一个用户名只能登录一次,同一用户名不能第二次登录。它工作正常。现在我想在服务器端使用 session 超时概念,但它不起作用,值正在存储到 session 中,但它无法获取另一个 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();

最佳答案

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

如果是这样,问题是您总是创建一个新的客户端,这会导致在服务器端产生新的 session ,因为存储在 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();

关于php - 服务器端 PHP session 在 android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30723668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com