[英]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.