I am editing this to clarify my question. users input a membername and displayname in a form, and the php looks for the membername in a list to match against, then edits the display name when successfully finding it with the user's input.
membernames and usernames are stored in the file like:
membername=displayname
This code works, except if they mistype their membername (meaning it doesn't match what is in the file), it still redirects to the success page, however it does not change the file (as it shouldn't).
What I need is to display an error page when they type in a membername that does not match, however I cannot get it to work.
<?
$fileurl = '/../../.../..../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) {
$parts = explode('=', $line);
if ($parts[0] == $membername) {
$file[$n] = $membername . '=' . $displayname;
}
}
file_put_contents($fileurl, implode("\n", $file)); // Put file back together
$success_page = 'http://example.com/.../private.php';
header('Location: '.$success_page);
?>
edited coding that I've tried : Using this coding below I've attempted to use it as is and modifying it. If I put in an error page to use a redirect it goes to the error page whether the script runs or not. On successful change it never redirects to google (using google just as a test).
<?
$fileurl = '/.../..../...../...../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) {
$parts = explode('=', $line);
if ($parts[0] == $membername) {
$file[$n] = $membername . '=' . $displayname;
//write the contents and redirect here itself
file_put_contents($fileurl, implode("\n", $file));
$success_page = 'http://google.com';//simply redirecting to google if its true
header('Location: '.$success_page);
}
else {//throw an error
//use this
$flag = 1;
}
if(isset($flag)){
//die("Membername not found");
header('Location:'.$error_page);
}
}
?>