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 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM