简化php用户配置文件更新代码

时间:2016-03-02 16:33:20

标签: php

我正在制作一个用户配置文件更新php文件,我想知道,有没有其他方法来简化这个if语句代码,检查输入是否为空?目前代码如下所示:

`

<?php 
    session_start();
    $name = test_input($_POST['name']);
    $surname = test_input($_POST['surname']);
    $password = $_POST['password'];
    $newPassword = $_POST['newPassword'];
    $newConfirmPassword = $_POST['newConfirmPassword'];

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        $data = strtolower($data);
        return $data;
    };

    if($password != $_SESSION['password']){
        die('Wrong password, try again!');
    }else{
        if(empty($name) && empty($surname) && empty($newPassword) && empty($newConfirmPassword)){
            die('No changes has been made!');   
        }else if(!empty($name) && empty($surname) && empty($newPassword) && empty($newConfirmPassword)){
            //name update
        }else if(!empty($name) && !empty($surname) && empty($newPassword) && empty($newConfirmPassword)){
            //name and surname update
        }else if(!empty($name) && !empty($surname) && !empty($newPassword) && empty($newConfirmPassword)){
            die('New passwords do not match');
        }else if(!empty($name) && !empty($surname) && empty($newPassword) && !empty($newConfirmPassword)){ 
            die('New password do not match');
        }else if(!empty($name) && empty($surname) && !empty($newPassword) && !empty($newConfirmPassword)){
            //name and passwords update
        }else if(!empty($name) && !empty($surname) && !empty($newPassword) && !empty($newConfirmPassword)){
            //name , password and surname update
        }else if(empty($name) && !empty($surname) && empty($newPassword) && empty($newConfirmPassword)){
            //surname update
        }else if(empty($name) && !empty($surname) && !empty($newPassword) && empty($newConfirmPassword)){
            die('New passwords do not match');
        }else if(empty($name) && !empty($surname) && empty($newPassword) && !empty($newConfirmPassword)){
            die('New passwords do not match');
        }else if(empty($name) && !empty($surname) && !empty($newPassword) && !empty($newConfirmPassword)){
            //surname and password change
        }else if(empty($name) && empty($surname) && !empty($newPassword) && empty($newConfirmPassword)){
            die('New passwords do not match');
        }else if(empty($name) && empty($surname) && empty($newPassword) && !empty($newConfirmPassword)){
            die('New passwords do not match');
        }else if(empty($name) && empty($surname) && !empty($newPassword) && !empty($newConfirmPassword)){
            //password change
        };
    };
?>

`

也许有办法以某种方式用函数做到这一点?

4 个答案:

答案 0 :(得分:1)

session_start();
if($_POST['password'] !== $_SESSION['password']){
    die('Wrong password, try again!');
}
$data['name'] = test_input($_POST['name']);
$data['surname'] = test_input($_POST['surname']);
$passwordConfirmed = $_POST['newConfirmPassword'] === $_POST['newPassword'] && !empty($_POST['newPassword']);
$newPassword = $_POST['newPassword'];
$data = array_filter($data);
if (count($data)===0) { 
   die('No changes has been made!'); 
} 
if (!empty($newPassword) && $passwordConfirmed) {
   //Update password
} else {
   foreach ($data as $field => $value) {
        //Update field. This would work best if your $field variable makes updating easy to code.
       //break; //If you only want to update one field on each request
   }
}

这看起来略小。

答案 1 :(得分:0)

您可以编写一个可以更新的功能a)名称b)姓氏c)密码。之后你可以这样做:

if(!empty($name)){
  updateName($name);
}
if(!empty($surname)){
  updateSurname($surname);
}
if(!empty($newPassword) && !empty($newConfirmPassword)){
  if($newPassword == $newConfirmPassword){
    updatePassword($newPassword);
  }
}

答案 2 :(得分:0)

我会使用多个功能:一个用于查看是否完全没有进行任何更改,一个用于测试密码是否有效等等。

示例代码:

function stripName($data) {
    return strtolower(htmlspecialchars(stripslashes(trim($data)));
}

function noChanges($name, $surname, $newPassword, $newConfirmPassword) {
    if(empty($name) &&
       empty($surname) &&
       empty($newPassword) &&
       empty($newConfirmPassword)
    ) {
        return 'No changes has been made!';
    }
    return false;
}

function oldPasswordIsInvalid($password) {
    if($password !== $_SESSION['password']){
         return 'Wrong password, try again!';
    }
    return false;
}

function newPasswordIsInvalid($password1, $password2) {
    if(empty($password1) {
        return 'Password must not be empty';
    }
    if(empty($password2) {
        return 'Password confirmation must not be empty';
    }
    if($password1 !== $password2) {
        return 'Passwords do not match';
    }
    return false;
}

function errorFound(
        $name,
        $surname,
        $password,
        $newPassword,
        $newConfirmPassword
) {
    return 
        oldPasswordIsInvalid($password) || 
        noChanges($name, $surname, $newPassword, $newConfirmPassword) || 
        newPasswordIsInvalid($newPassword, $newConfirmPassword);
}

function removeEmptyFieldsFromArray($array) {
    $returnArray = [];
    foreach($array as $key => $value) {
        if(!empty($value)) {
            $returnArray[$key] = $value;
        }
    }
    return $returnArray;
}

session_start();
$name = stripName($_POST['name']);
$surname = stripName($_POST['surname']);
$password = $_POST['password'];
$newPassword = $_POST['newPassword'];
$newConfirmPassword = $_POST['newConfirmPassword'];

if ($errorMessage = errorFound(
        $name,
        $surname,
        $password,
        $newPassword,
        $newConfirmPassword
)) {
    die($errorMessage);
} else {
    $valuesThatNeedToBeUpdated = removeEmptyFieldsFromArray([
        'name' => $name,
        'surname' => $surname,
        'password' => $newPassword
    ]);
    // $valuesThatNeedToBeUpdated is an array that contains
    // only the fields that aren't empty.
    // Fields in this array are the ones that need to be updated
}

答案 3 :(得分:0)

我认为John Slegers&#39;答案非常好。这是程序应该如何运作的方式。

在我看来,它可以实现更多&#34;优雅&#34;与exceptions。使用exceptions时的优点是,使用嵌套函数,您不必检查每个级别的返回值并将其传递 - 而不是这样,您可以仅在一个点实现最终流程并且定义良好

考虑花这本书花费大约33美元Clean Code by Martin。有一些好主意。你可能不同意所有这些,但它为你提供了一个很好的指导方法。

正如John在评论中指出的那样,尝试使用多个函数并赋予函数非常好的名称。 30岁以前,你不是一个程序员,你的功能和变量名称有2个字符...

这是一个非常好的迹象,你问这个问题,因为你不满意&#34;与您的版本。