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 关于用abap来解决动态规划的问题,但是要求输出索引值,这个是难点
  • ¥15 在ISIS中什么是IP从地址
  • ¥15 压测时,并发量过高时,响应时间出现尖刺
  • ¥15 关于vmprotect3.8.4虚拟文件一项
  • ¥15 在不用IT调试的情况下怎样能连外网
  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面
  • ¥15 CCS离散化传递函数与仿真不一致
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法