Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Cannot get cookie

Greetings,

I'm trying to get the PHPSESSID cookie, but it keeps returning null. I'm certain the page login.php gives out a cookie as I've seen the cookie in my web browser/Firebug. Here is the cookie from Firebug: PHPSESSID=7atro6i6rc7ugprt4841g5nej2

Appcelerator (Android Simulator 2.2 API with 1.4.0 sdk) gives an error "cannot call method 'getValue' of null".

Here is my code:

var xhr=Titanium.Network.createHTTPClient();
xhr.onload=function(){
    if(xhr.responseText=="OK"){
        alert(xhr.getCookie("PHPSESSID").getValue());   //<----- does not work
        //Apparently xhr.getCookie("PHPSESSID") is null
        var win_home=Titanium.UI.createWindow({
            url:'wins/hoome.js',
            backgroundColor:'#CCC'
        });
        alert(Titanium.App.eoh_cookie);
        win_home.open();
    }
};
xhr.onerror=function(){
    alert('There was an error processing your request. Please try again.');
}

xhr.open("POST","http://10.0.2.2/Dropbox/www/ajax/login.php");
var data={};
data.uname=uname.value;
data.pword=pword.value;
xhr.send(data);

Am I getting the cookie in the right place? Any insight greatly appreciated.

Many thanks in advance,

— asked November 17th 2010 by Eamonn Hynes
  • cookie
  • cookies
  • httpclient
0 Comments

3 Answers

  • Ok, I've managed to resolve this problem:

    Get a cookie with:

    var cookie=xhr.getResponseHeader("Set-Cookie");
    //parse cookie if necessary
    

    Set a cookie withi:

    ...
    xhr.setRequestHeader("Cookie",cookie);
    //just before send
    xhr.send(data);
    

    Many thanks for all the help guys,

    — answered November 17th 2010 by Eamonn Hynes
    permalink
    2 Comments
    • glad to help out

      — commented November 17th 2010 by Aaron Saunders
    • use cookie = xhr.getResponseHeader("Set-Cookie");
      I got a cookie like this :
      user_info=%23%3CUser%3A0x000000067a8f40%3E; path=/; expires=Fri, 06 Mar 2015 02:54:32 -0000
      how can I parse the user_info?

      — commented February 6th 2015 by gml gml
  • set cookie should be

    cdb_auth="XXXXX"
    cdb_sid="XXXX"

    xhr.setRequestHeader('Cookie', cdb_auth);
    xhr.setRequestHeader("Cookie", cdb_sid);

    since cookie transfers as http request header

    — answered November 25th 2010 by Samwise Zhan
    permalink
    0 Comments
  • this will get you all of the cookies, I guess then you can parse the string to get a specific one

    xhr.onload=function(){
        if(xhr.responseText=="OK") {
            Ti.API.info( "cookies: " + xhr.getResponseHeader('Set-Cookie') );
        }
    
    — answered November 17th 2010 by Aaron Saunders
    permalink
    1 Comment
    • Hey, I got that. Here is my code to parse the cookie:

      function getSessID(cookie){
          var split1=cookie.split(';');
          var split2=split1[0].split('PHPSESSID=');
          return split2[1];
      }
      

      Which is called with:

      Ti.App.PHPSESSID=getSessID(xhr.getResponseHeader("set-cookie"));
      

      But I can't seem to successfully set the cookie now…

      ...
      xhr.open("POST","http://10.0.2.2/Dropbox/www/ajax/login.php");
      xhr.setCookie("PHPSESSID",Ti.App.PHPSESSID);   //<-- this line won't work!
      var data={};
      data.uname=uname.value;
      data.pword=pword.value;
      xhr.send(data);
      

      Using appcelerator 1.4.3. When I try it in Appcelerator 1.5, it says the function 'setCookie()' doesn't exist?!

      Many thanks in advance,

      — commented November 17th 2010 by Eamonn Hynes
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.