创建解锁/锁定页面功能

时间:2013-01-05 18:54:33

标签: php html

我正在尝试创建锁定和解锁页面功能。用户必须按此顺序浏览页面:

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

那么应该发生的是,如果用户在页面上他们应该打开,那么该页面将被解锁(或者换句话说,如果用户访问,则会在显示页面代码的地方满足if语句)一个他们不应该打开的页面,然后该页面被beocmes锁定(在显示带有Continue超链接的div的div时会遇到else语句。)

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

下面是一个示例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
}
?>

以下是完整的steps.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;
}

?>

2 个答案:

答案 0 :(得分:3)

像这样的东西,虽然这可能不会那样:

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

基本上,根据您的“步骤”数组,您可以像下一样在会话中存储允许页面的索引。当他们完成一个页面并“解锁”下一页时,您将在会话中增加该索引值并重定向到序列中的下一页。

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

答案 1 :(得分:1)

保持简单,似乎你已经过度复杂了目标。您似乎只想确保用户完成流程的先前步骤,然后才能继续执行下一步。为什么不尝试更像......

// 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为脚本名称提供索引号。然后,您只需在“解锁”此页面之前检查以前的索引是否已标记为已完成。