1

What i Need:

  • i Need to use user and evnt_id from cookie.

    document.cookie
    

    string :

    " country=India; countryCode=IN; __gads=ID=369fde524de77341:T=1417077062:S=ALNI_MakhfG3fYeRIwt9Uw4xSUE8m-2hHw; fbm_172404889604820=base_domain=.10times.com; PHPSESSID=4e467l46efdmvc9vktknod85b2; evnt_id=9; industry=74; evnt_name=India International Trade Fair; evnt_url=iitf; km_ai=jE0JIU3hamljHh0DrWRC%2FR50QNI%3D; km_lv=x; __zlcmid=SAeGECzmK8Nrve; user_flag=2; user=13530767; _ga=GA1.2.1767513107.1417003918; linkedin_oauth_ro57ogahnixy_crc=null; km_uq=; kvcd=1418207265523; _ga=GA1.3.342868374.1416999745; id=13530767; email=afeef1915%40yahoo.com; name=Mohd+Afeef; image_flag=http%3A%2F%2Fgraph.facebook.com%2F100001729894039%2Fpicture".
    
  • i need to fetch

    • user.
    • event_id from cookie.

code i have tried :

function setCookie(key, value) {  
var expires = new Date();  
expires.setTime(expires.getTime() + 31536000000); //1 year  
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();

}  

   function getCookie(key) {  
     var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');

     return keyValue ? keyValue[2] : null;  
       }  

    setCookie('evnt_id','evnt_id');  
   alert(getCookie('evnt_id')); 
  • problem im facing im not able to fetch user and event_id from cookie.
  • im using symphony framework and need to implement in twig file.

1 Answer 1

2

This problem has been solved before. Don't reinvent the wheel :)

I would advise you to read up on cookies: http://www.quirksmode.org/js/cookies.html

The code below comes from that article.

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

console.log(readCookie('evnt_id'));  //9
console.log(readCookie('user'));     //13530767
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.