Skip to content

Instantly share code, notes, and snippets.

@dvlden
Last active May 2, 2024 06:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save dvlden/3ed3c022d1508eae3d2d3cdabafa06f3 to your computer and use it in GitHub Desktop.
Save dvlden/3ed3c022d1508eae3d2d3cdabafa06f3 to your computer and use it in GitHub Desktop.
Quickly Extract "Netscape HTTP Cookie File" to "JSON" (from ".txt" to ".json" format)
<?php
function extractCookies ($string) {
$cookies = array();
$lines = explode("\n", $string);
// iterate over lines
foreach ($lines as $line) {
// we only care for valid cookie def lines
if (isset($line[0]) && substr_count($line, "\t") == 6) {
// get tokens in an array
$tokens = explode("\t", $line);
// trim the tokens
$tokens = array_map('trim', $tokens);
$cookie = array();
// Extract the data
$cookie['domain'] = $tokens[0];
$cookie['flag'] = (bool) $tokens[1];
$cookie['path'] = $tokens[2];
$cookie['secure'] = (bool) $tokens[3];
// Convert date to a readable format
$cookie['expiration'] = date('Y-m-d h:i:s', $tokens[4]);
$cookie['name'] = $tokens[5];
$cookie['value'] = $tokens[6];
// Record the cookie.
$cookies[] = $cookie;
}
}
return $cookies;
}
<?php
require('extract-cookies.php'); // require the file from above...
if (isset($_FILES['file'])) {
$inputName = $_FILES['file']['tmp_name'];
$contents = file_get_contents($inputName);
$outputName = 'cookies.json';
$convertContents = extractCookies($contents);
file_put_contents($outputName, json_encode($convertContents));
if (file_exists($outputName)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($outputName).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($outputName));
readfile($outputName);
unlink($outputName);
exit;
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Convert Cookies from "txt" to "json" format</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container my-5">
<p class="lead mb-5">
Browse an `cookies.txt` for upload <i>(file name doesn't matter)</i> and begin converting.
</p>
<form method="POST" enctype="multipart/form-data">
<label class="btn btn-lg btn-block btn-secondary">
<input type="file" id="file" name="file" hidden>
Browse
</label>
<button type="submit" class="btn btn-block btn-primary">Convert!</button>
</form>
</div>
</body>
</html>
@onemans-battle
Copy link

onemans-battle commented Dec 6, 2018

thanks.
And I think there's something to think about:
1, flag and secure will always equal to bool true because the string is 'TRUE' or 'FALSE'
$flag = strtolower($tokens[1]) == 'true' ? true : false;
2, Comment lines may also have six tabs
ltrim($line)[0] != '#'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment