doujing6436 2014-10-28 23:46
浏览 45
已采纳

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

My login script displays an error message after pressing the reg button, and I can only get one echo message back.

Edit one

Is it meaningful to use elseif or should i use if ... else ...?

Edit two

After the program has finished, how can I display all available errors the program encountered under the input boxes?


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\">
";
    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条回答 默认 最新

  • dsadsadsa1231 2014-10-29 03:44
    关注

    Edit One

    Yes, you should use elseif, it makes your code look cleaner.

    Edit Two

    You can use GET method to return your errors with switch statement and foreach loops.

    Either that or you can use jQuery to check for errors and configure it without PHP.

    jQuery Validation Implementation

    EDIT:

    $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!";
        }
    }
    

    I hope this clears your doubt on foreach loops.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line