将页面限制为特定会话 ID
Restrict Page to Specific Session ID
我编写了一个简单的登录系统,并且可以正常工作。我设置了一些仅在登录时可见的页面。我想将页面限制为特定会话 ID。我该怎么做呢?这就是我现在用来限制页面的方法:
<?php
session_start();
if (!isset($_SESSION['u_id'])) {
header("Location: ../index.php?index=mustlogin");
exit();
}
?>
我如何将其从任何 u_id 限制到特定的 u_id?
您可以创建特定 ID 的数组,然后使用 in_array 来验证用户。
例子
<?php
session_start();
$sessionIds = array('1','2'); //for example i have inserted 1 and 2 as ids
if (!isset($_SESSION['u_id']) || in_array($_SESSION['u_id'], $sessionIds)) {
header("Location: ../index.php?index=mustlogin");
exit();
}
说明
我在这里创建了一个 $sessionIds 数组,其中包含不允许访问页面的特定 ID。然后检查 in_array 当前会话用户 ID 存在于 $sessionIds 数组中,然后重定向到用户。
您需要将您的 $_SESSION['uid'] 与您的特定 ID 相匹配。为此,您需要特定用户 ID 的某种数据。有多种方法可以做到这一点,但我会用数组来做到这一点。您需要的是一组特定的 ID
//Should've come from database of your users
$specific= array(
"id" => 1
);
然后通过in_array()
在数组中搜索
if (!in_array($_SESSION['u_id'], $specific)) {
header("Location: ../index.php?index=mustlogin");
exit();
}
我编写了一个简单的登录系统,并且可以正常工作。我设置了一些仅在登录时可见的页面。我想将页面限制为特定会话 ID。我该怎么做呢?这就是我现在用来限制页面的方法:
<?php
session_start();
if (!isset($_SESSION['u_id'])) {
header("Location: ../index.php?index=mustlogin");
exit();
}
?>
我如何将其从任何 u_id 限制到特定的 u_id?
您可以创建特定 ID 的数组,然后使用 in_array 来验证用户。
例子
<?php
session_start();
$sessionIds = array('1','2'); //for example i have inserted 1 and 2 as ids
if (!isset($_SESSION['u_id']) || in_array($_SESSION['u_id'], $sessionIds)) {
header("Location: ../index.php?index=mustlogin");
exit();
}
说明
我在这里创建了一个 $sessionIds 数组,其中包含不允许访问页面的特定 ID。然后检查 in_array 当前会话用户 ID 存在于 $sessionIds 数组中,然后重定向到用户。
您需要将您的 $_SESSION['uid'] 与您的特定 ID 相匹配。为此,您需要特定用户 ID 的某种数据。有多种方法可以做到这一点,但我会用数组来做到这一点。您需要的是一组特定的 ID
//Should've come from database of your users
$specific= array(
"id" => 1
);
然后通过in_array()
在数组中搜索if (!in_array($_SESSION['u_id'], $specific)) {
header("Location: ../index.php?index=mustlogin");
exit();
}