2017-01-25 158 views
2

我实际上在网上找不到任何答案。在编码方面,我一直在网上搜索答案。那么..PHP替换文件中的特定行

我想知道如何在PHP中使用别的东西来更改文件中的特定行?

我有4行看起来像这样:

$2y$10$hashedpassword 
typewar 
typewarMMM36634573323 
84.2xx.xxx.xxx 

我最近已经开始寻找到饼干,这些东西,并希望能够更改cookie(3号线),每当到了人的日志服务器或去index.php

我猜这样的事情?

$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'); 

请帮忙。

+0

当您尝试该代码时会发生什么? –

+0

@AndrewMortimer没有线索,因为这只是我认为代码可能看起来像的一个例子。我有一段时间挣扎着,我不想再测试这个了。哈哈 – Typewar

回答

0

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

$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); 
+0

我该如何执行这个功能? – Typewar

+0

只是将代码片段与'implode'函数之前和之后的'echo $ lines;'集成在一起。 – Naga

+0

okey,我试过了:echo implode('/ n',$ lines_arr1);它给了我cookie,现在我怎么把它保存到第3行的文件? – Typewar