下面我有一个应该发生的场景:
- User在
create_session.php
页面上,因此数组中的其他页面为锁定 - 如果用户试图访问另一个页面,则显示div
- 如果用户选择继续链接,那么它应该将用户导航到他们应该在
create.php
页
问题是,如果用户访问另一个页面并出现div框,如果用户单击Continue
链接,它不会导航回create_session.php
页面,或者换句话说,用户最后访问的页面。
点击Continue
链接后,url似乎没有改变
为什么在点击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行
注意:未定义变量:steps in…第1636行
下面这行显示注意事项:
<a href="<?php echo $steps[$latestIdx + 1] ?>">Continue</a>
当我导航到一个被锁定的页面时(换句话说,当你必须使用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
}
?>
下面是完整的步骤。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;
}
?>
This:
if ($currentIdx - $latestIdx > 1 ) {
return 1;
} else {
return 0;
}
我认为应该是这样的:
if ($currentIdx - $latestIdx >= 1 )
{
return true;
}
return false;
和不确定这是从哪里来的:
$pages[$currentPages+1]
您的steps数组也需要在函数外部声明如果您计划在url中使用它,则必须返回$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;
}
和
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
}
?>