Okay. In that case, if we don't particularly care what the contents of username.txt is, we can simply replace it everytime with a file containing the new details. You can put this all in one script if you like. The following script does the job, but it would not produce perfect results for names that contain an apostrophe.
<?php
// Grab POST variables and prefix with p_
//
import_request_variables('p', 'p_');
// A username variable for simplicity in the code
$namefile = 'username.txt';
?>
<html>
<head>
<title>Testing username function</title>
</head>
<body>
<?php
if(isset($p_username))
{
// We have been sent a username variable, so change the contents of username.txt, and advise the user
// Write the username - this overwrites the contents of the file if it already exists
file_put_contents ($namefile, $p_username);
// We're going to be echoing the username back next, so need to do a little sanity check:
$p_username = htmlentities($p_username);
// Confirm
?>
<p>
Thank you, <?php echo $p_username; ?>. Your user name has
been updated.
</p>
<?php
} else {
// No user name is set; the form has not been used
// Grab the user name from the file, if it exists; if not, set it to '-undefined-'
if (file_exists($namefile))
{
$username = file_get_contents($namefile);
} else {
$username = '-undefined-';
}
// Again, we're going to be echoing the username back next, so do the sanity check:
$username = htmlentities($username);
// Display a form to enable name changing
?>
<p>
Welcome. The username is currently <?php echo $username; ?>.
Use the form below to change this.
</p>
<form method='POST'>
Username: <input type='text' name='username'>
<input type='submit' value='Go!'>
</form>
<?php
}
?>
</body>
</html>