PHP读取cookie文件 [英] PHP reading a cookie file

查看:123
本文介绍了PHP读取cookie文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何帮助库libs读取php文件。我在我的本地磁盘上有一个cookie文件,我想要一个更好的阅读方式。我目前正在逐行读取文件,并解析出值。

Are there any helper libs to read a cookie file in php. I have a cookie file on my local disk and I would like a better way of reading it. I am currently just reading to file like by line and parsing out the values.

推荐答案

Netscape的格式(例如curl以这种格式保存在COOKIEJAR中的Cookie)。

This is pretty simple if you aim to read Netscape's format (e.g. curl saves cookies in COOKIEJAR in this format).

首先是一个示例(管道和行号在这里添加,不会出现在真实文件中) / p>

First an example (pipes and line numbers are added here and do not occur in real file):

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 |

如您所见:


  1. 我们可能有以表示的注释行作为第一个字符。

  2. 我们可能有空行

  1. We might have comment lines denoted with # as the first character.
  2. We might have blank lines

然后,每行有7个令牌,分隔一个制表符( \t )。
这些是此处定义的

And then, the beefy lines each have 7 tokens seprated with a tab character (\t). These are defined here:


  1. domain - 创建AND并可读取变量的域。

  2. flag - 一个TRUE / FALSE值,表示给定域中的所有计算机是否都可以访问该变量。此值由浏览器自动设置,具体取决于您为域设置的值。

  3. path - 变量对其有效的域中的路径。

  4. secure - 一个TRUE / FALSE值,指示是否需要与域的安全连接来访问变量。

  5. expiration - 变量将到期的UNIX时间。 UNIX时间定义为自1970年1月1日00:00:00 GMT以来的秒数。

  6. name - 变量的名称。

  7. value - 变量的值。

  1. domain - The domain that created AND that can read the variable.
  2. flag - A TRUE/FALSE value indicating if all machines within a given domain can access the variable. This value is set automatically by the browser, depending on the value you set for domain.
  3. path - The path within the domain that the variable is valid for.
  4. secure - A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable.
  5. expiration - The UNIX time that the variable will expire on. UNIX time is defined as the number of seconds since Jan 1, 1970 00:00:00 GMT.
  6. name - The name of the variable.
  7. value - The value of the variable.

文件解析器。

// 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>';

最后,此处< a>是演示。

Finally, here is a demo.

这篇关于PHP读取cookie文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆