简体   繁体   English

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

[英]How to route the user to the client portal using PHP

I'm trying to route the user to client_portal.php if they go to login.html when already logged in. 我正在尝试将用户路由到client_portal.php,如果他们在登录时转到login.html。

  • Tried creating a third .php file that included the session info and tested if the login_user was set 尝试创建包含会话信息的第三个.php文件,并在设置了login_user时进行测试

  • Tried using jQuery "window.location.replace();" 尝试使用jQuery“window.location.replace();” to automatically route the user everytime 每次自动路由用户

This is the code from the third PHP file, checkLogin.php 这是来自第三个PHP文件checkLogin.php的代码

include("session.php");

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

This is from sessions.php -- DB is a placeholder for the actual info (which is private) 这是来自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();
    } 

Expected: Route the user to the client portal Actual: Loads the login page as normal 预期:将用户路由到客户端门户实际:正常加载登录页面

You can create middlewares for the App, like middleware for login check or maybe for permissions . 您可以为App创建中间件 ,例如用于登录检查的中间件或可能用于权限

Here is Answers about what is middlewares What is middleware in laravel? 以下是关于什么是中间件的答案什么是laravel中的中间件?

you are wrongly setting the session variable. 你错误地设置了会话变量。 Here is the correct way. 这是正确的方法。

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

I thought i'd post my answer incase anyone wants to use this for their project: loggedIn.php 我想我会发布我的答案,任何人都想将它用于他们的项目:loggedIn.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"];

The main login page: 主登录页面:

    include("loggedIn.php");

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

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

相关问题 需要从 azure 门户获取每个用户的客户端 ID,并希望使用 php 存储在变量中 - Need to get client id of each user from azure portal and want to store in a variable using php 如何生成 Azure O365 API 以获取我们使用 php 在 azure 门户网站上创建的用户详细信息 - How to generate Azure O365 APIs to get user details which we have created on azure portal using php 使用PHP Curl重定向到我的学校门户 - Using PHP Curl to redirect to my school portal 如何使用 .htaccess 路由 Php 文件 - How to Route Php Files Using .htaccess 如何使用动态用户代理 header 使用 PHP Curl 或 Guzzel 客户端 - How to use dynamic user-agent header using PHP Curl or Guzzel Client PHP Portal类似于java门户 - PHP Portal similar to java portal 无法使用Google PHP API客户端库更新用户信息 - Cannot update user information using Google PHP API Client library 是否可以使用“官方PHP客户端库”在Facebook上更改用户状态? - Is it possible to change the user status on Facebook, using the 'Official PHP Client Library'? 使用php客户端库检索Google用户信息 - Retrieve google user info using php client library 使用SOAP客户端从PHP在Dynamics CRM上创建用户帐户 - Creating a User Account on Dynamics CRM from PHP using SOAP client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM