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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?