2

I have a file with usernames and displaynames store in it like this:

testname=displayname<br>
testname2=displayname2<br>
etc=etc<br>

This list is done twice on the same file, same usernames and same displaynames. I need to replace them in both areas in the lists (needs to be replaced twice)

I am trying to create a form where uses can change their display name. I was trying to copy my code from a page where it looked up passwords for their accounts in a file, found it, and replaced it, however it doesn't seem to be working for this file.

The form for changing their names is simple, it has them enter in their member name (so I could use that to have it find their name in the list) and then uses what they input for a display name to change their display name in the file.

Form Page Code:

<center>Change Display Name:<p>
<form action="http://example.com/xxx/displaynamesave.php" class="form" method="post">
<input "membername" name="membername" /><p><input "displayname" name="displayname" /><p><input name="Submit" type="submit" /></form></p>/center>

and below is my php code for processing

<?
$fileurl = '/xxx/myfiles/sitename/xxx/memberfiletest';

$membername = $_POST['membername'];
$displayname = $_POST['displayname']; 

$file = file($fileurl, FILE_IGNORE_NEW_LINES); // Get file as array of lines

foreach ($file AS $n=>$line)
if (substr($line, 0, 20) === '$membername') // Line starts with 'membername'
    $file[$n] = '$membername = '.$displayname; // Replace displayname

file_put_contents($fileurl, implode("\n", $file)); // Put file back together
$success_page = 'http://example.com/thisplace/xxx/xxx/successredirector.html';
header('Location: '.$success_page);
?>

When I input the data and hit submit, it goes to my success page, however it doesn't make any changes in the proper file and I'm unsure how to tell what I'm missing.

3 Answers 3

3

First, your comparison shows the variable in single quotes ', which means PHP won't parse it and is in fact comparing everything to $membername, and not the value of the variable $membername. Change the comparison to:

foreach ($file as $n => $line) {
    if (substr($line, 0, 20) === $membername) { // Line starts with 'membername'
        $file[$n] = $membername . ' = ' . $displayname; // Replace displayname
    }
}

Second, in the example of the contents of your file, the "username" portion are not all the same length ("testname", "etc"), but your comparison is checking against the first 20 characters of the line. If the format is indeed username=displayname, you would probably have better results splitting the line on the = (there are a couple ways to do this, of course), and comparing the first part. An example would be something like :

foreach ($file as $n => $line) {
    $parts = explode('=', $line);
    if ($parts[0] == $membername) {
        $file[$n] = $membername . ' = ' . $displayname;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, thank you Davis I appreciate that because its easier to check against the first 20 characters in a line when using a predetermined word like "password" where you know it has 8 characters, being able to split it based on their membername and username with the = makes it even easier
0

Use strpos

foreach ($file as $n => $line) {
    if (strpos($line, $membername) === 0) { // Line starts with 'membername'
        $file[$n] = $membername . ' = ' . $displayname; // Replace displayname
    }
}

2 Comments

Notice, if you have 2 similar names like john and johns, you'll get unwanted effects (johns can be overwritten by john). See stackoverflow.com/a/35695745/487923 to avoid it.
you could then just: strpos($line, $membername . "=") === 0 to avoid this problem.
0

You're using variables inside a '. Try this:

foreach ($file as $n => $line) {
    list($name, $dname) = explode('=', trim($line));
    if ($name === $membername) { // Line starts with 'membername'
        $file[$n] = $membername . ' = ' . $displayname; // Replace displayname
    }
}

2 Comments

Completely missed that, thank you ksimka, sadly making that change still goes through, but doesn't make any changes to the file
Yes, because there are other errors. See stackoverflow.com/a/35695833/487923

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.