PHP 替换文件中的特定行

PHP replace specific line in file

我实际上在网上找不到任何答案。在编码方面,我一直在网上搜索答案。嗯..

我想知道如何用 PHP 中的其他内容更改文件中的特定行?

我有 4 行如下所示:

y$hashedpassword
typewar
typewarMMM36634573323
84.2xx.xxx.xxx

我最近开始研究 cookie 和那些东西,并且希望能够在用户登录服务器或访问 index.php

时更改 cookie(第 3 行)

我猜是这样的?

$user = 'typewar';
$GeneratedCookie = $user . 'MMM' . date('H', time()) * date('i', time()) * 333;
$lines = file('/path/to/identity/' . $user . '/identity.txt');
str_replace($lines[2], $GeneratedCookie . '/n');

请帮忙。

我不太确定你的情况。请找到以下想法并查看。

$user = 'typewar';
$GeneratedCookie = $user . 'MMM' . date('H', time()) * date('i', time()) * 333;
$lines = file('/path/to/identity/' . $user . '/identity.txt');
print_r($lines); 
$lines[2] = $GeneratedCookie;
print_r($lines);