douyanyan1123 2014-05-16 05:10
浏览 160
已采纳

匹配并替换文本文件的某一行

I am trying to setup a way where users can change the password to a certain area of a website they have access too and that password is stored as part of a .cfg text file. That is the file I need to pull it from. The contents of the file looks like this:

[main]
roomname = "Room Name"
topfile = /my/link/here
bannerfile = /my/link/here
bannersfile = /my/link/here
banner_freq = 40
bodyfile = /my/link/here
configfile = /my/link/here
actionfile = /my/link/here
memberfile = /my/link/here
moderatorfile = /my/link/here
logfile = /my/link/here
bootfile = /my/link/here
numusers = 30
password = mypassword
defaultmessage = "$USER$ : $SAYS$"
messagewrapper = '<TABLE WIDTH="100%" border=0 cellpadding=0 cellspacing=0><TR><TD>($DATESTAMP$ : $TIMESTAMP$) $PROVE$ $REGISTERED$ $MOD$ $MESSAGE$</TD></TR></TABLE><P>'

I have the change password form created, however what I am having trouble with is the .php file to process the form. I've been reading manuals but am not quite sure what else is missing or certain variables to use.

====

Edit/Update

I am updating my code below with my save.php form. It seems to be working except that its overwriting the entire file blankly and I'm not sure why its doing that. I'm also including my form coding from the send page.

<?
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$data = $_POST['password'];
$filevar = '/my/site/location/here/prpwtest/prtest.txt';
$fh = fopen($filevar, "w");
$file_contents = file_get_contents($filevar);
$lines = explode ("", $file_contents); // split
for ($i = 0; $i < count($lines); $i++) { // for all lines
if (strpos($lines[$i], 'password = ') === 0)
fwrite($fh, 'password = '.$data.''); // put new password instead of old one
else
fwrite($fh, $lines[$i]); // keep old line
$success_page = '/my/site/location/here/pulldowns/savesuccessful.html';

header('Location: '.$success_page);
}
}

?>

And here is a snippet of my save code:

<form name="loginform" method="post" action="my/site/location/here/prpwtest/prpwtestsave.php">
<input name="password" type="password" /><p><input name="Submit" type="submit" /></form></p></center>
</div>
  • 写回答

3条回答 默认 最新

  • doujiu3095 2014-05-16 05:28
    关注

    Give this a try:

    $fileurl = '/path/to/my/file/here/file.cfg';
    
    $replace = 'what I want to put in there when they submit'; 
    
    $file = file($fileurl, FILE_IGNORE_NEW_LINES); // Get file as array of lines
    
    foreach ($file as $n=>$line)
        if (substr($line, 0, 8) === 'password') // Line starts with 'password'
            $file[$n] = 'password = '.$replace; // Replace password line
    
    file_put_contents($fileurl, implode("
    ", $file)); // Put file back together
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作