dongshan4878 2019-04-03 07:44
浏览 81
已采纳

php登录脚本无法正常工作,它不会重定向回仪表板

i have not been able to get this, i have been on this for days ,i dont know if its from the login page or the dashboard , but my login script doesnt redirect to the dashboard

i have tried alot of things and even added an echo at every line of code , it shows that , the scripts get to the redirect and actually redirects but still comes back to the login page

<?php require 'sqlconnect.php' ?>

<?php
    session_start(); 
?>
<?php

    $email= '';
    $password = '';

    $errors = '';
    $success = '';

    if(isset($_REQUEST['login'])){

        $email = $_REQUEST['email'];
        $password = $_REQUEST['password'];

        if(empty($email)){
            $errors .= '<div class="alert alert-danger" role="alert">
                        Email field is empty.
                    </div>';
            }
        if(empty($password)){
            $errors .= '<div class="alert alert-danger" role="alert">
                        Password field is empty.
                    </div>';
        }
        if(!empty($email) and !empty($password)){

            $confirm_info = mysqli_query($connection, "SELECT * FROM users WHERE email = '$email' AND password = '$password'");

            if(mysqli_num_rows($confirm_info) >= 1){
                echo "if statement";

                while($results = mysqli_fetch_assoc($confirm_info)) {
                    $token = $results['token'];
                    $token = $_SESSION['token'];
                    header("Location: dashboard/profile.php");
                }
            }else{
                $errors .= '<div class="alert alert-danger" role="alert">
                            Login credentials doesn\'nt exist is our database.
                        </div>';
            }
        }
    }
?>
</head>
<body>


        <form class="login100-form validate-form" method="POST" action="login.php">
            <span class="login100-form-title p-b-37">
                Sign In
                <!-- <i class="fa fa-home uk-align-center uk-text-center"></i> -->
            </span>
            <?php
                echo $errors;
                //echo $success;
            ?>
            <br>

            <div class="wrap-input100 validate-input m-b-20" data-validate="Enter username or email">
                <input class="input100" type="email" name="email" placeholder="email" value="<?php echo $email ?>">
                <span class="focus-input100"></span>
            </div>

            <div class="wrap-input100 validate-input m-b-25" data-validate = "Enter password">
                <input class="input100" type="password" name="password" placeholder="password">
                <span class="focus-input100"></span>
            </div>

            <div class="container-login100-form-btn">
                <button class="login100-form-btn" id="login" name="login">
                    Sign In
                    <i class="fa fa-spinner fa-spin" id="spinner" style="display: none"></i>
                </button>
            </div>
            <div class="text-center p-t-57 p-b-20">
                <span class="txt1">
                <a href="forgetpassword.php" class="txt2 hov1">
                    forget password
                    <i class="fa fa-long-arrow-right m-l-5"></i>
                </a>
                </span>
            </div>

            <div class="text-center p-t-57 p-b-20">
                <span class="txt1">
                    Or Create  an account
                </span>
            </div>


            <!-- <div class="flex-c p-b-112">
                <a href="#" class="login100-social-item">
                    <i class="fa fa-facebook-f"></i>
                </a>

                <a href="#" class="login100-social-item">
                    <img src="login/images/icons/icon-google.png" alt="GOOGLE">
                </a>
            </div> -->
<!-- 
            <div class="text-center">
                <a href="#" class="txt2 hov1">
                    Sign Up
                </a>
            </div> -->
        </form>
        <div class="container-login100-form-btn">
            <a href="signup.php" class="txt2 hov1 text-light">
                <button class="signin100-form-btn">
                   Sign Up
                </button>
            </a>
        </div>

    </div>
</div>

</body>
</html>

And this is the dashboard profile page code

<?php require '../sqlconnect.php' ?>
<?php 
  session_start();
  $token = $_SESSION['token'];
    if(!isset($_SESSION['token'])){
        header("Location: ../login.php");
        exit();
    }else{

      $all_details = mysqli_query($connection, "SELECT * FROM users WHERE token = '$token'");

      if(mysqli_num_rows($all_details)){

        $results = mysqli_fetch_assoc($all_details);
        $id = $results['id'];
        $token = $results['token'];
        $email = $results['email'];
        $first_name = $results['first_name'];
        $is_admin = $results['is_admin'];
        $last_name = $results['last_name'];
        $usernam = $results['username'];
        $image_name = $results['image_name'];
        $image_dir = $results['image_dir'];

        // echo $username;
        }
    }
?>
  • 写回答

3条回答 默认 最新

  • duanletao9487 2019-04-03 07:54
    关注

    The characters that are not in PHP quote <?php ?> are treated as HTML output. And if the PHP page send any HTML output to the browser, the header would be sent at the same time. That's why any header() call after closing tag ?> would be useless.

    You need to change your login page, or any page with header() calls to not close ?> before calling. For example:

    <?php
    
    require 'sqlconnect.php';
    session_start(); 
    
        $email= '';
        $password = '';
    
        $errors = '';
        $success = '';
    
        if(isset($_REQUEST['login'])){
    
            $email = $_REQUEST['email'];
            $password = $_REQUEST['password'];
    
            if(empty($email)){
                $errors .= '<div class="alert alert-danger" role="alert">
                            Email field is empty.
                        </div>';
                }
            if(empty($password)){
                $errors .= '<div class="alert alert-danger" role="alert">
                            Password field is empty.
                        </div>';
            }
            if(!empty($email) and !empty($password)){
    
                $confirm_info = mysqli_query($connection, "SELECT * FROM users WHERE email = '$email' AND password = '$password'");
    
                if(mysqli_num_rows($confirm_info) >= 1){
                    echo "if statement";
    
                    while($results = mysqli_fetch_assoc($confirm_info)) {
                        $token = $results['token'];
                        $token = $_SESSION['token'];
                        header("Location: dashboard/profile.php");
                    }
                }else{
                    $errors .= '<div class="alert alert-danger" role="alert">
                                Login credentials doesn\'nt exist is our database.
                            </div>';
                }
            }
        }
    ?>
    </head>
    <body>
    
    
            <form class="login100-form validate-form" method="POST" action="login.php">
                <span class="login100-form-title p-b-37">
                    Sign In
                    <!-- <i class="fa fa-home uk-align-center uk-text-center"></i> -->
                </span>
                <?php
                    echo $errors;
                    //echo $success;
                ?>
                <br>
    
                <div class="wrap-input100 validate-input m-b-20" data-validate="Enter username or email">
                    <input class="input100" type="email" name="email" placeholder="email" value="<?php echo $email ?>">
                    <span class="focus-input100"></span>
                </div>
    
                <div class="wrap-input100 validate-input m-b-25" data-validate = "Enter password">
                    <input class="input100" type="password" name="password" placeholder="password">
                    <span class="focus-input100"></span>
                </div>
    
                <div class="container-login100-form-btn">
                    <button class="login100-form-btn" id="login" name="login">
                        Sign In
                        <i class="fa fa-spinner fa-spin" id="spinner" style="display: none"></i>
                    </button>
                </div>
                <div class="text-center p-t-57 p-b-20">
                    <span class="txt1">
                    <a href="forgetpassword.php" class="txt2 hov1">
                        forget password
                        <i class="fa fa-long-arrow-right m-l-5"></i>
                    </a>
                    </span>
                </div>
    
                <div class="text-center p-t-57 p-b-20">
                    <span class="txt1">
                        Or Create  an account
                    </span>
                </div>
    
    
                <!-- <div class="flex-c p-b-112">
                    <a href="#" class="login100-social-item">
                        <i class="fa fa-facebook-f"></i>
                    </a>
    
                    <a href="#" class="login100-social-item">
                        <img src="login/images/icons/icon-google.png" alt="GOOGLE">
                    </a>
                </div> -->
    <!-- 
                <div class="text-center">
                    <a href="#" class="txt2 hov1">
                        Sign Up
                    </a>
                </div> -->
            </form>
            <div class="container-login100-form-btn">
                <a href="signup.php" class="txt2 hov1 text-light">
                    <button class="signin100-form-btn">
                       Sign Up
                    </button>
                </a>
            </div>
    
        </div>
    </div>
    
    </body>
    </html>
    

    You'd need similar changes in the profile page, but you should have got the idea from the above example.

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

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低