I make an webserver with lamp(linux+apache+mysql+php).I want to set cookie to hold user information such as userid,username.So the load.php is like this:
....
///login successful
session_start();
$_COOKIE["userid"]=$userid;
$response["success"]=1;
$response["message"]="load successful";
echo json_encode($response);
...
In Android LoadActivity I create a class WebLoader extends AsyncTask,and I override the function String doInBackground(String... params)
like this:
client=new DefaultHttpClient();
HttpPost post=new HttpPost(url_up);
...
HttpResponse response=client.execute(post);
HttpEntity entity=response.getEntity();
CookieStore mCookieStore = client.getCookieStore();
List<Cookie> cookies = mCookieStore.getCookies();
if(cookies.isEmpty())
Log.d("cookie","cookie is none");
for (int i = 0; i < cookies.size(); i++) {
if("userid".equals(cookies.get(i).getName()))
{
userid=Integer.parseInt(cookies.get(i).getValue());
}
}
When I run the app, logcat says:cookie is none. Why Cookie is none? I have already set it at load.php.
You should set cookies with setcookie()
. For your example:
setcookie("userid", $userid, time() + 60*60*24, "/");
This will set a cookie named userid
with the value $userid
and will expire 31 days from when it is set.