简体   繁体   English

无法重定向到 PHP 中的另一个页面(登录功能)

[英]Can't redirected to another page in PHP (Login function)

I have create a login page.我已经创建了一个登录页面。 Other page will show after user logged in. But the problem is it can't redirected to my index.php page after i click the login button.. Anyone can help me?用户登录后将显示其他页面。但问题是它无法重定向到我的 index.php 页面后我单击登录按钮.. 任何人都可以帮助我吗? and the index.php also cannot shown.并且 index.php 也无法显示。 TTT index page is not working. TTT 索引页不工作。 I have check all the database but it is no problem...我检查了所有数据库,但没问题...

include ('header.php');

//check user in session function
if(!empty($_SESSION['user'])) {

    //redirect user if already logged in
    echo"<script>window.location='user/index.php';</script>";
    die;
}

//login function
//check login form submission
if (isset($_POST['login'])) {

    //check empty form for email and password
    if (empty($_POST['email']) || empty($_POST['password'])) {

        //give message fail for empty form input
        echo'
            <div class="overlay">
                <div class="popup">
                    <a class="close" href="account.php">&times;</a>
                    <div class="content">
                        <h2>Failed</h2>
                        <p>Please Fill Your Username or Password</p>
                    </div>
                </div>
            </div>
        ';
    
    } else {

        //set value in variable
        $email = $_POST['email'];
        $password = $_POST['password'];

        //security purpose
        $email2 = stripslashes($email);
        $password2 = stripslashes($password);
        //escape all symbol that input in form
        $email3 = $connection->real_escape_string($email2);
        $password3 = $connection->real_escape_string($password2);

        //convert password as md5 encrypted hash
        $password4 = md5($password3);

        //get user detail
        $sql="SELECT userID,userEmail,userPassword,role FROM user_sc WHERE userEmail='$email3' AND userPassword='$password4'";
        $result = $connection->query($sql);
        $verify = $result->num_rows;

        //check user exist or not
        if ($verify == 1) {

            //fecth user detail to get their role
            $getID = $result->fetch_array();

            //set value in variable
            $id = $getID['userID'];
            $role = $getID['role'];

            //check user role
            if ($role == 'CUSTOMER') {

                //put and hold userID and role inside session
                $_SESSION['user'] = $id;
                $_SESSION['role'] = $role;
                
                //redirect page after success login
                header("location:user/index.php");

            } elseif ($role == 'ADMIN') {

                //put and hold userID and role inside session
                $_SESSION['user'] = $id;
                $_SESSION['role'] = $role;
                
                //redirect page after success login
                header("location:admin/index.php");

            } else {

                //give fail message for unauthorized user role
                echo'
                    <div class="overlay">
                        <div class="popup">
                            <a class="close" href="account.php">&times;</a>
                            <div class="content">
                                <h2>Failed</h2>
                                <p>Unauthorized user attempt</p>
                            </div>
                        </div>
                    </div>
                ';

            }

        } else {

            //give message if email and password not match
            echo'
                <div class="overlay">
                    <div class="popup">
                        <a class="close" href="account.php">&times;</a>
                        <div class="content">
                            <h2>Failed</h2>
                            <p>Email/Password Are Not Recognize</p>
                        </div>
                    </div>
                </div>
            ';
               
        }
        
    }

}

In place where you use echo"<script>window.location='user/index.php';</script>";在你使用echo"<script>window.location='user/index.php';</script>";地方replace on header('Location: user/index.php')替换header('Location: user/index.php')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM