Reputation: 77
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.
====
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 ("\r", $file_contents); // split
for ($i = 0; $i < count($lines); $i++) { // for all lines
if (strpos($lines[$i], 'password = ') === 0)
fwrite($fh, 'password = '.$data.'\r'); // 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>
Upvotes: 0
Views: 94
Reputation: 7447
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("\n", $file)); // Put file back together
Upvotes: 1
Reputation: 5651
Try this.
$array = parse_ini_file('your_ini_file.ini', 'main');
$array['main']['password'] = 'set your password here';
$str = '[main]'."\r\n";
foreach($array['main'] as $key => $value){
$str .= $key.' = '.$value."\r\n";
}
file_put_contents('test.ini', $str, LOCK_EX);
Upvotes: 1
Reputation: 4689
You need to parse data from $file_contents. I.e. after reading of this string, you have to process it line by line to find string starting with 'password=' to modify it. You can use explode() for splitting of this string:
$lines = explode ("\r", $file_contents); // split
for ($i = 0; $i < count($lines); $i++) { // for all lines
if (strpos($lines[$i], 'password = ') === 0)
fwrite($fh, 'password = '.$newpassword.'\r'); // put new password instead of old one
else
fwrite($fh, $lines[$i]); // keep old line
}
Upvotes: 1