创建解锁/锁定页面特性


Creating an unlock/lock page feature

我正在尝试创建类似于锁和锁页功能的东西。用户必须按照以下顺序浏览页面:

$steps = array(1 =>'create_session.php',2 => 'QandATable.php',3 => 'individualmarks.php',4 => 'penalty.php',5 => 'penaltymarks',6 => 'complete.php');

所以应该发生的是,如果用户在一个他们应该在的页面上,那么这个页面应该被解锁(或者换句话说,if语句在它显示页面代码的地方被满足),如果用户访问一个他们不应该在的页面,那么这个页面就被锁定(else语句在它显示Continue超链接'的地方被满足)。

问题是,即使用户在正确的页面上,当页面应该被解锁以便用户可以使用该页时,该页仍然被"锁定"。目前,所有访问的页面都被锁定,所以我的问题是,当用户在正确的页面上时,我如何解锁页面?

下面是一个示例create_session.php:
 <?php
session_start();
include ('steps.php'); //exteranlised steps.php
?>
<head>
...
</head>
<body>
<?php
if ((isset($username)) && (isset($userid))) { //checks if user is logged in
    if (allowed_in() === "Allowed") {
        //create_session.php code:
    } else {
        $page = allowed_in() + 1;
?>
 <div class="boxed">
<a href="<?php echo $steps[$page] ?>">Continue with Current Assessment</a>
<?php
    }
} else {
    echo "Please Login to Access this Page | <a href='./teacherlogin.php'>Login</a>";
    //show above echo if user is not logged in
}
?>
下面是完整的步骤。php:
<?php
$steps = array(1 =>'create_session.php',2 => 'QandATable.php',3 => 'individualmarks.php',4 => 'penalty.php',5 => 'penaltymarks',6 => 'complete.php');
function allowed_in($steps = array()){
// Track $latestStep in either a session variable
// $currentStep will be dependent upon the page you're on
if(isset($_SESSION['latestStep'])){
   $latestStep = $_SESSION['latestStep'];
}
else{
   $latestStep = 0;
}
$currentStep = basename(__FILE__); 
$currentIdx = array_search($currentStep, $steps);
$latestIdx = array_search($latestStep, $steps);
if ($currentIdx - $latestIdx == 1 )
    {
       $currentIdx = $_SESSION['latestStep'];
       return 'Allowed';
    }
    return $latestIdx;
}
?>

就像这样,尽管这可能无法正常工作:

$allowed_page = $_SESSION['latestStep'];
if ($steps[$allowed_page] == $_SERVER['SCRIPT_NAME']) {
   ... allowed to be here ...
}

基本上,给定"steps"数组,您存储会话中允许的页面的索引。当它们完成一个页面并"解锁"下一页时,您可以在会话中增加该索引值并按顺序重定向到下一页。

if ($page_is_done) {
    $_SESSION['latestStep']++;
    header("Location: " . $steps[$_SESSION['latestStep']]);
}

保持简单,似乎你的目标过于复杂了。您似乎只是想确保用户在继续下一个步骤之前完成了流程的前一个步骤。

// General Idea
$completedArr = array('1' => false, '2' => false ...);
$pageMap = array('page1.php' => '1', 'page2.php' => '2' ...);
// On Page1
$completedArr = $_SESSION['completedArr'];
$locked = true;
$currentStep = $pageMap[$_SERVER['SCRIPT_NAME']];  // '1'
if($currentStep > 1)
{
    if($completedArr[$currentStep - 1] === true)
        $locked = false;
}
else
{
    $locked = false;
}
$completedArr[$currentStep] = true;
$_SESSION['completedArr'] = $completedArr;

如果连续页面也需要使用此选项。其思想是,您将定义的pageMap为脚本名称提供索引号。然后,在"解锁"此页面之前,您只需检查之前的索引是否标记为已完成。