[ create a new paste ] login | about

Link: http://codepad.org/d1POiPIx    [ raw code | output | fork ]

aka - PHP, pasted on Oct 23:
<?php

/**
 * Extract any cookies found from the cookie file. This function expects to get
 * a string containing the contents of the cookie file which it will then
 * attempt to extract and return any cookies found within.
 *
 * @param string $string The contents of the cookie file.
 * 
 * @return array The array of cookies as extracted from the string.
 *
 */
function extractCookies($string) {

    $lines = explode(PHP_EOL, $string);
  
    foreach ($lines as $line) {
        
        $cookie = array();
        
        // detect httponly cookies and remove #HttpOnly prefix
        if (substr($line, 0, 10) == '#HttpOnly_') {
	    $line = substr($line, 10);
            $cookie['httponly'] = true;
        } else {
            $cookie['httponly'] = false;
        } 
     
        // we only care for valid cookie def lines
        if($line[0] != '#' && substr_count($line, "\t") == 6) {
 
            // get tokens in an array
            $tokens = explode("\t", $line);
 
            // trim the tokens
            $tokens = array_map('trim', $tokens);
 
            // Extract the data
            $cookie['domain'] = $tokens[0]; // The domain that created AND can read the variable.
            $cookie['flag'] = $tokens[1];   // A TRUE/FALSE value indicating if all machines within a given domain can access the variable. 
            $cookie['path'] = $tokens[2];   // The path within the domain that the variable is valid for.
            $cookie['secure'] = $tokens[3]; // A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable.
 
            $cookie['expiration-epoch'] = $tokens[4];  // The UNIX time that the variable will expire on.   
            $cookie['name'] = urldecode($tokens[5]);   // The name of the variable.
            $cookie['value'] = urldecode($tokens[6]);  // The value of the variable.
   
            // Convert date to a readable format
            $cookie['expiration'] = date('Y-m-d h:i:s', $tokens[4]);
  
            // Record the cookie.
            $cookies[] = $cookie;
        }
    }

    return $cookies;
}

// in real code we'd read from a file
// $lines = file('./cookie.txt');

/**
 * we assume this is in our file
 **/
$f = "
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
 
.google.com	TRUE	/	FALSE	1305843382	cookiename	the value
.yahoo.com	TRUE	/	FALSE	1305843382	another_cookie	it's value
#HttpOnly_google.com	TRUE	/	FALSE	1305843382	httponly_cookie	another value
";

var_dump(extractCookies($f));

?>


Output:
array(3) {
  [0]=>
  array(9) {
    ["httponly"]=>
    bool(false)
    ["domain"]=>
    string(11) ".google.com"
    ["flag"]=>
    string(4) "TRUE"
    ["path"]=>
    string(1) "/"
    ["secure"]=>
    string(5) "FALSE"
    ["expiration-epoch"]=>
    string(10) "1305843382"
    ["name"]=>
    string(10) "cookiename"
    ["value"]=>
    string(9) "the value"
    ["expiration"]=>
    string(19) "2011-05-19 10:16:22"
  }
  [1]=>
  array(9) {
    ["httponly"]=>
    bool(false)
    ["domain"]=>
    string(10) ".yahoo.com"
    ["flag"]=>
    string(4) "TRUE"
    ["path"]=>
    string(1) "/"
    ["secure"]=>
    string(5) "FALSE"
    ["expiration-epoch"]=>
    string(10) "1305843382"
    ["name"]=>
    string(14) "another_cookie"
    ["value"]=>
    string(10) "it's value"
    ["expiration"]=>
    string(19) "2011-05-19 10:16:22"
  }
  [2]=>
  array(9) {
    ["httponly"]=>
    bool(true)
    ["domain"]=>
    string(10) "google.com"
    ["flag"]=>
    string(4) "TRUE"
    ["path"]=>
    string(1) "/"
    ["secure"]=>
    string(5) "FALSE"
    ["expiration-epoch"]=>
    string(10) "1305843382"
    ["name"]=>
    string(15) "httponly_cookie"
    ["value"]=>
    string(13) "another value"
    ["expiration"]=>
    string(19) "2011-05-19 10:16:22"
  }
}


Create a new paste based on this one


Comments: