PHP登录脚本无法处理页面可用错误

时间:2014-10-28 23:46:07

标签: php messages

我的登录脚本在按下reg按钮后显示错误消息,我只能收到一条回送消息。

编辑一个

使用elseif或使用if ... else ...是否有意义?

编辑两个

程序完成后,如何在输入框下显示程序遇到的所有可用错误?


PHP

    <?php
if($_POST['reg'] == "Registrieren"){
    $email = $_POST['email'];
    $username  = $_POST['user']; 
    $password  = $_POST['pw'];
    $passwordrep = $_POST['pwrep'];

    $email = stripslashes($email);
    $username  = stripslashes($username); 
    $password  = stripslashes($password);
    $passwordrep = stripslashes($passwordrep);

    /*if(isset($_SESSION['log']) OR $_SESSION['log'] == 1){
        $error['already_online'] = 1;
        $error['main'] = 1;
    }else*/if(empty($username)){
        $error['username_empty'] = 1;
        $error['main'] = 1;
    }else{
        if(empty($email)){
            $error['email_empty'] = 1;
            $error['main'] = 1;
        }else{
            if(empty($password) OR empty($passwordrep)){
                $error['passwords_empty'] = 1;
                $error['main'] = 1;
            }else{
                if($password == $passwordrep){
                    $error['password_confirm'] = 1;
                    $error['main'] = 1;
                }else{
                    if(strlen($username) < 6 OR strlen($username) > 64){
                        $error['username_too_short'] = 1;
                        $error['main'] = 1;
                    }else{
                        if(strlen($password) < 8 OR strlen($passwordrep) < 8){
                            $error['password_too_short'] = 1;
                            $error['main'] = 1;
                        }else{
                            if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
                                $error['email_invalid'] = 1;
                                $error['main'] = 1;
                            }else{
                                if(!preg_match('/^[a-z\d]{2,64}$/i', $username)){
                                    $error['username_invalid'] = 1;
                                    $error['main'] = 1;
                                }else{
                                    if($error['main'] = 0){
                                    include_once("config/db_config.php");

                                    $email = mysqli_real_escape_string($con, $email);
                                    $username  = mysqli_real_escape_string($con, $username); 
                                    $password  = mysqli_real_escape_string($con, $password);
                                    $passwordrep = mysqli_real_escape_string($con, $passwordrep);

                                    $sql = "SELECT * FROM hp_user WHERE username = '$username'";
                                    $query = mysqli_query($con, $sql) or die($msg_signup["signup_no_connection_aviable"]);
                                    $row = mysqli_num_rows($query);
                                    if($row == 1){
                                        $error['username_exists'] = 1;
                                        $error['main'] = 1;
                                    }else{
                                        $sql = "SELECT * FROM hp_user WHERE email = '$email'";
                                        $query = mysqli_query($con, $sql) or die($msg_signup["signup_no_connection_aviable"]);
                                        $row = mysqli_num_rows($query); 
                                        if($row == 1){
                                            $error['email_exists'] = 1;
                                            $error['main'] = 1;
                                        }else{
                                            $_SESSION['log'] = 1;
                                            $_SESSION['user'] = $query->username;
                                            $_SESSION['email'] = $query->email;
                                            $add = mysqli_query($con,"INSERT INTO `agptest`.`hp_user` (`ID`, `username`, `email`, `password`, `date`, `locked`, `permission`) VALUES (NULL, '$username', '$email', '$passwordrep', CURRENT_TIMESTAMP, '0', '1');"); 
                                            $error['main'] = 0;
                                            mysqli_close();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
?>

<?php
if ($error['main'] == "0") {
    header("Location: http://www.allgameplay.de/?site=default");
}else{
    echo "<h1>Registrierung</h1>";
    echo "<form method=\"post\" action=\"index.php\">\n";
    echo "Benutzername:<br><input name=\"user\" type=\"text\" size=\"30\"><br>";
    if($error['username_empty'] == 1){
        echo $msg_signup['signup_user_empty'];
    }
    if($error['username_too_short'] == 1){
        echo $msg_signup['signup_user_too_short'];
    }
    if($error['username_invalid'] == 1){
        echo "Dein Benutzername ist ungültig!";
    }
    echo "<br>E-Mail Adresse:<br><input name=\"email\" type=\"text\" size=\"30\"><br>";
    if($error['email_empty'] == 1){
        echo "Du hast keine E-Mail Adresse angegeben!";
    }
    if($error['email_invalid'] == 1){
        echo "Dies ist keine richtige E-Mail Adresse!";
    }
    echo "<br>Passwort:<br><input name=\"pw\" type=\"password\" size=\"30\"><br>";
    if($error['passwords_empty'] == 1){
        echo "Du hast kein Passwort angegeben!";
    }
    echo "<br>Passwort wiederholen:<br><input name=\"pwrep\" type=\"password\" size=\"30\"><br>";
    if($error['passwords_empty'] == 1){
        echo "Du hast kein Passwort angegeben!";
    }
    if($error['password_confirm'] == 1){
        echo "Die Passw&ouml;rter stimmen nicht überein!";
    }
    echo "<br><input type=\"submit\" name=\"reg\" value=\"Registrieren\"><br>";
}
?>

2 个答案:

答案 0 :(得分:0)

替换

if($password == $passwordrep)

if($password != $passwordrep)

你需要在if之前声明错误数组($ _ POST [&#39; reg&#39;] ==&#34; Registrieren&#34;)

答案 1 :(得分:0)

编辑一个

是的,你应该使用elseif,它会让你的代码看起来更干净。

编辑两个

您可以使用GET方法通过switch语句和foreach循环返回错误。

或者您可以使用jQuery检查错误并在没有PHP的情况下进行配置。

jQuery Validation Implementation

修改

$error = array('abc','def', 'ghi');
foreach ($error as $i) {
    switch ($i) {
        case "abc":
            echo $i;
        case "def":
            echo $i;
        case "ghi":
            echo $i;
        default:
            echo "No case found!";
    }
}

我希望这能清除你对foreach循环的怀疑。