如何使用PHP将用户路由到客户端门户

时间:2019-06-15 19:24:35

标签: php jquery session login

如果他们已经登录,则尝试将用户路由到client_portal.php。

  • 试图创建第三个包含会话信息的.php文件,并测试是否设置了login_user

  • 尝试使用jQuery“ window.location.replace();”每次自动路由用户

这是来自第三个PHP文件checkLogin.php的代码

include("session.php");

    if (isset($_SESSION['login_user'])) {
        header("Location: client_portal.php");
        die();
    }

这是来自sessions.php -DB是实际信息的占位符(私有)

    $link = mysqli_connect('DB', 'DB', 'DB', 'DB'); //Connect to the database

    session_start();    

    //Preparing the SQL statements
    $stmt1 = mysqli_prepare($link,"SELECT username, first_name FROM login WHERE username =?");

    //Binding the SQL statements
    mysqli_stmt_bind_param($stmt1, "s", $user_check);

    $user_check = $_SESSION['login_user']; //Checking if the user has a login session active
    mysqli_stmt_execute($stmt1);

    $result = mysqli_stmt_get_result($stmt1); //Get the result

    $row = mysqli_fetch_assoc($result); //Setting the array

    $_SESSION["login_session"] = $row['username']; //Setting a session under the username
    $firstName = $row["first_name"];

    if(!isset($_SESSION['login_user'])) {
        header("Location: /login");
        die();
    } 

预期:将用户路由到客户端门户 实际:正常加载登录页面

3 个答案:

答案 0 :(得分:0)

您可以为该应用创建middlewares,例如用于登录检查或也许用于权限的中间件。

以下是有关中间件What is middleware in laravel?

的答案

答案 1 :(得分:0)

您错误地设置了会话变量。 这是正确的方法。

$_SESSION['login_session'] = $row['username']; //Setting a session under the username

答案 2 :(得分:0)

我以为我会把答案贴出来,以防有人想将其用于他们的项目:loginIn.php

$link = mysqli_connect('DB', 'DB', 'DB', 'DB'); //Connect to the database

    if (!$link) { 
        die('Could not connect: ' . mysqli_connect_error()); 
    }

    session_start();

    $stmt = mysqli_prepare($link,"SELECT logged_in FROM login WHERE username=?");
    mysqli_stmt_bind_param($stmt, "s", $user_check);

    if(!isset($_SESSION['login_user'])) { //Checking if the session is not set
        $user_check = ""; //Make the $user_check null
    } else { //if
        $user_check = $_SESSION['login_user'];
    }

    mysqli_stmt_execute($stmt);

    $result = mysqli_stmt_get_result($stmt);
    $row = mysqli_fetch_assoc($result);

    $loggedIn = $row["logged_in"];

主要登录页面:

    include("loggedIn.php");

    if ($loggedIn == 1) {
        header("Location: client_portal.php");
        die();
    }