下面我有应该发生什么的情景:
create_session.php
页面上,因此数组中的其他页面是
锁定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
链接时,在网址中显示以下两个错误:
注意:未定义的变量:在1636行的......中的latestIdx
注意:未定义的变量:步骤1636行
以下此行显示通知:
<a href="<?php echo $steps[$latestIdx + 1] ?>">Continue</a>
更新2:
当我导航到一个被锁定的页面时(换句话说,当显示您必须Continue
的框时),我收到了未定义的$_SESSION
个变量,我也收到此错误:{ {1}}
对于Undefined variable: steps in steps.php on line 57
未定义的错误,是因为我将$ _SESSION变量放在错误的位置?如何在steps.php中停止未定义的步骤变量?
下面是代码示例的$_SESSION
示例:
QandATable.php
以下是完整的steps.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
}
?>
答案 0 :(得分:1)
此:
if ($currentIdx - $latestIdx > 1 ) {
return 1;
} else {
return 0;
}
我认为应该是这样的:
if ($currentIdx - $latestIdx >= 1 )
{
return true;
}
return false;
并且不确定这是来自:
$pages[$currentPages+1]
您的步骤数组也需要在函数外声明 如果你打算在网址中使用它,你必须返回$ latestIdx。我不确定你是想回去还是前进?
仍然不确定我是否理解你想要的东西,但这应该是一个开始:
$steps = array(1 =>'create_session.php',2 => 'QandATable.php',3 => 'individualmarks.php',4 => 'penalty.php',5 => 'penaltymarks',6 => 'complete.php');
function allowed_in($steps)
{
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;
}
和Page
if (allowed_in()=== "Allowed")
{
//create_session.php code
}
else
{
$page = allowed_in()+1;
?>
<div class="boxed">
<a href="<?php echo $steps[$page] ?>">Continue</a>
</div>
<?php
}
?>