0

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);
}
}
?>

3 Answers 3

1

Change from this

if ($parts[0] == $membername) {
    die("Membername not found");
} 
else {
    $file[$n] = $membername . '=' . $displayname;
}

to this

//when you match you put in array
if ($parts[0] == $membername) {
    $file[$n] = $membername . '=' . $displayname;
    //soon after you got a match put the contents in file and redirect
} 
else {//throw an error
    //die("Membername not found");
    //use this 
    $flag = 1;
}

if(isset($flag)){
   //do what you need to handle the error
}

Edit2: Try this link.

Sign up to request clarification or add additional context in comments.

3 Comments

Oddly enough when I did that, it just kills the script and displays an error page that says "Membername not found" even though I know I entered in a correct membername.
thank you narasimharaosp for the edits. I have it in there and I updated my coding in my original post so you can see how I put the flag coding to handle the error, am not sure if its correct, it still simply dies and shows the page "membername not found" no matter what i input.
Thank you for the edited link. When I do that, it goes through and works, however does not redirect to google, and if I use a bogus membername it doesn't redirect it just hangs on a white page which is the .php page its going to and doesn't make the changes on the backend in the file. So there's no re-direct and no real error page.
1
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("\r\n", $file));
    $page = 'http://google.com';//simply redirecting to google if its true
} 
else {//throw an error
   //use this 
   $page = $err_page;
}

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

5 Comments

Thank you Bhavik when I tried your code it came up and said invalid argument on line 7 foreach () I didn't change anything else so I'm unsure why that is I'm doublechecking my code.
I tried it again inputting your code directly and the only thing I changed was the $err_page to go someplace else so I could tell if it errored, and in both cases, it SKIPS the first redirect and whether on success or fail, it goes to whatever error page I input.
i used the same code and executed it. and it was not showing any error and running successfully. I used text file to store and retrieve the data.
It runs and executes the script, the problem seems to be that it runs all the way through the redirect page regardless of if wrong data is input or not. When a person inputs their membername and displayname, its supposed to find the membername and if so, then change the line it needs to. It does this correctly, however if a wrong membername is put in, it still goes through to the re-direct page,
however it does not make the changes in the file but it doesn't show an error page either. hopefully that makes sense. I need it to show an error if the membername they put in, does not match what is in the file.
0

I was able to finally resolve this by studying $flags more and ensuring that I had them written properly and in the correct context for when I wanted the checks to apply.

 <?
$fileurl = '/..../..../..../..../memberfiletest';

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

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

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://.../..../...html'; // redirect
                                                                                // if
                                                                                // successful
    header('Location: ' . $success_page);
    $found = 1;
}
}

if ($found == 0)
{
// No matching user... Redirect to error     
$error_page = 'http://google.com'; // redirecting to google just for
                                       // testing
header('Location:' . $error_page);
}
?>

Comments

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.