PHP if else语句错误检查

时间:2016-02-29 10:39:19

标签: php html

我正在编辑此内容以澄清我的问题。用户在表单中输入成员名称和显示名称,并且php在列表中查找要匹配的成员名称,然后在使用用户输入成功找到显示名称时编辑显示名称。

成员名和用户名存储在文件中,如:

membername=displayname

此代码有效,除非他们输入错误的成员名称(意味着它与文件中的内容不匹配),它仍会重定向到成功页面,但它不会更改文件(因为它不应该)。

我需要的是当他们输入一个不匹配的成员名时显示错误页面,但是我无法让它工作。

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

我尝试过编辑的编码:使用下面的编码,我试图按原样使用并修改它。如果我输入错误页面以使用重定向,则无论脚本是否运行,它都会转到错误页面。成功更改后,它永远不会重定向到谷歌(使用谷歌作为测试)。

<?
$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 个答案:

答案 0 :(得分:1)

从此更改

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

到这个

//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:试试这个link

答案 1 :(得分: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);
}

答案 2 :(得分:0)

我能够通过更多地学习$ flags并确保我在正确的情况下正确编写它们来解决这个问题,以便我能够应用这些检查。

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