|
function save_to_cookiejar($cookiefile) |
|
{ |
|
$jar = ""; |
|
foreach ($_COOKIE as $name => $value) |
|
{ |
|
$name = urlencode($name); |
|
$value = urlencode($value); |
|
$jar .= "{$_SERVER['HTTP_HOST']}\tFALSE\t/\tFALSE\t0\t{$name}\t{$value}\n"; |
|
} |
|
file_put_contents($cookiefile, $jar); |
|
} |
|
|
|
/** |
|
* Source: http://stackoverflow.com/a/26521309/276152 |
|
*/ |
|
function load_from_cookiejar($cookiefile) |
|
{ |
|
$jar = file_get_contents($cookiefile); |
|
foreach (explode(PHP_EOL, $jar) 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 && $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[1] === 'FALSE' ? '.' : '') . $tokens[0]; // The domain that created AND can read the variable. |
|
$cookie['path'] = $tokens[2]; // The path within the domain that the variable is valid for. |
|
$cookie['secure'] = $tokens[3] === 'TRUE'; // A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable. |
|
$cookie['expire'] = $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. |
|
|
|
setcookie($cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httponly']); |
|
} |
|
} |
|
} |