下面我有应该发生的情况:
- 用户在
create_session.php
页面上,因此阵列中的其他页面被锁定 - 如果用户尝试访问另一个页面,则会显示上面显示的 div 框:
- 如果用户选择继续链接,那么它应该将用户导航到他们应该所在的
create.php
页面
问题是,如果用户访问另一个页面并出现 div 框,如果用户单击该Continue
链接,它不会导航回该create_session.php
页面或换句话说用户上次访问的页面。
Continue
点击链接后,网址似乎没有改变
为什么点击后没有将用户导航到他们应该在的页面Continue
?
尝试控制哪些页面被锁定并保持用户所在页面未锁定的脚本位于此脚本steps.php
中,如下所示:
<?php
function allowed_in(){
$steps = array('create_session.php', 'QandATable.php', 'individualmarks.php', 'penalty.php', 'penaltymarks', 'complete.php');
// 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 = "";
}
$currentStep = basename(__FILE__);
$currentIdx = array_search($currentStep, $steps);
$latestIdx = array_search($latestStep, $steps);
if ($currentIdx - $latestIdx > 1 ) {
return true;
} else {
return false;
}
}
?>
现在存储在数组中的每个脚本都包含代码include('steps.php')
并包含以下代码:
<?php
if (allowed_in()){
//create_session.php code
}else{
?>
<div class="boxed">
<a href="<?php echo $steps[$latestIdx + 1] ?>">Continue</a>
</div>
<?php
}
?>
更新:
当我单击Continue
链接时,在 url 中显示了两个错误,如下所示:
注意:未定义的变量:latestIdx in ... 第 1636 行
注意:未定义的变量:第 1636 行的 ...
下面显示了此行的通知:
<a href="<?php echo $steps[$latestIdx + 1] ?>">Continue</a>
更新 2:
当我导航到一个被锁定的页面时(换句话说,当你必须Continue
出现的框,我得到未定义的$_SESSION
变量,我也得到这个错误:Undefined variable: steps in steps.php on line 57
对于$_SESSION
未定义的错误,是因为我将 $_SESSION 变量放在错误的位置吗?以及如何停止steps.php中未定义的步骤变量?
下面是代码的示例QandATable.php
顺序,如下所示:
<?php
ini_set('session.gc_maxlifetime',12*60*60);
ini_set('session.gc_divisor', '1');
ini_set('session.gc_probability', '1');
ini_set('session.cookie_lifetime', '0');
require_once 'init.php';
//12 hours sessions
session_start();
include('steps.php'); //exteranlised steps.php
<head>
if (isset($_POST['id'])) {
$_SESSION['id'] = $_POST['id'];
}
if(isset($_POST['sessionNum'])){
//Declare my counter for the first time
$_SESSION['initial_count'] = $_POST['sessionNum'];
$_SESSION['sessionNum'] = intval($_POST['sessionNum']);
$_SESSION['sessionCount'] = 1;
}
elseif (isset($_POST['submitDetails']) && $_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
$_SESSION['sessionCount']++;
}
</head>
<body>
<?php
//once session is expired, it should log the user out, but at mo this isn't happening
if ((isset($username)) && (isset($userid))){ //checks if user is logged in
if (allowed_in()=== "Allowed"){
//QandATable.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){
// 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;
}
?>