Advertisement
bpoole

Convert JSON cookie to Netscape cookie

Oct 1st, 2013
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. function bake_cookies($dest, $src){
  2.     //covert JSON cookie file (src) to Netscape cookie file (dest)
  3.     $cookie_json = json_decode(preg_replace('/^#.*\n/', '', file_get_contents($src))); //get cookie json contents, strip #-style comments, decode
  4.  
  5.     $netscape_cookie = ''; //build netscape cookie contents
  6.     foreach($cookie_json as $cookie){
  7.         $secure_flag = ($cookie->secure) ? 'TRUE' : 'FALSE';
  8.         $expiration = 0;
  9.         if(isSet($cookie->expirationDate)){
  10.             $expiration = round($cookie->expirationDate);
  11.         }
  12.         $netscape_cookie .= "{$cookie->domain}\tFALSE\t{$cookie->path}\t{$secure_flag}\t{$expiration}\t{$cookie->name}\t{$cookie->value}\n";
  13.     }
  14.  
  15.     file_put_contents($dest, $netscape_cookie); //create netscape cookie using accumulated cookies
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement