是否有任何帮助库来读取PHP中的cookie文件.我的本地磁盘上有一个cookie文件,我想要一个更好的阅读方式.我目前只是按行阅读文件并解析出值.
Maj*_*our 29
如果你的目标是阅读Netscape的格式(例如curl以这种格式保存COOKIEJAR中的cookie),这非常简单.
首先是一个例子(管道和行号在这里添加,不会出现在实际文件中):
01 | # Netscape HTTP Cookie File
02 | # http://curl.haxx.se/rfc/cookie_spec.html
03 | # This file was generated by libcurl! Edit at your own risk.
04 |
05 | .google.com TRUE / FALSE 1305843382 cookiename the value
06 | .yahoo.com TRUE / FALSE 1305843382 another_cookie it's value
07 |
Run Code Online (Sandbox Code Playgroud)
如你看到的:
#
为第一个字符.然后,强壮的线条每个都有7个标记,用制表符分隔(\t
).这些被定义在这里:
所以,现在让我们制作我们的cookie文件解析器.
// read the file
$lines = file('path/to/cookies.txt');
// var to hold output
$trows = '';
// iterate over lines
foreach($lines as $line) {
// 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);
// let's convert the expiration to something readable
$tokens[4] = date('Y-m-d h:i:s', $tokens[4]);
// we can do different things with the tokens, here we build a table row
$trows .= '<tr></td>' . implode('</td><td>', $tokens) . '</td></tr>' . PHP_EOL;
// another option, make arrays to do things with later,
// we'd have to define the arrays beforehand to use this
// $domains[] = $tokens[0];
// flags[] = $tokens[1];
// and so on, and so forth
}
}
// complete table and send output
// not very useful as it is almost like the original data, but then ...
echo '<table>'.PHP_EOL.'<tbody>'.PHP_EOL.$trows.'</tbody>'.PHP_EOL.'</table>';
Run Code Online (Sandbox Code Playgroud)
最后,这是一个演示.
小智 9
我找不到一个代码来满足现在非常流行的HttpOnly cookie记录 - 例如在http://www.google.com/中使用.HttpOnly cookie只能由浏览器读取,并且对所有客户端脚本(如java脚本)保持隐藏状态.您可以在此处找到有关HttpOnly cookies的更多详细信息.
忽略Netscape cookie文件中这些cookie的格式将导致解析不正确.这些记录以"#HttpOnly_"字符串为前缀.
我们还应该为几乎所有应用程序"urldecode"参数名称及其值.
下面的函数是的样本代码的组合和更新的版本马吉德Fouladpour和菲利普诺顿它参考的HttpOnly饼干和与它们在阵列的属性返回所有的Cookie:
/**
* 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( strlen( $line ) > 0 && $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;
}
Run Code Online (Sandbox Code Playgroud)
这是该功能的演示.
归档时间: |
|
查看次数: |
20290 次 |
最近记录: |