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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?