2
當我導航到一個被鎖定的頁面時(換句話說,當你想要出現Continue
這個框的時候,我得到了一個未定義的$_SESSION
變量。在我包含if (allowed_in()=== "Allowed"){
語句之前,我沒有收到任何未定義的$ _SESSION變量,但是因爲現在需要的是if語句,即時通訊開始得到這些變量的錯誤。
對於$_SESSION
未定義的錯誤,是不是因爲我將在$ _SESSION變量在錯誤的地方?
下面是一個例子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>
<?php
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 = 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;
}
?>
爲什麼這些'的ini_set()'不是在你的php.ini或.htaccess文件?在代碼中使用'ini_set()'用於您想要動態更改的設置;不是那些你想在每次運行php時都設置的方式。 – Spudley
您在PHP代碼中間有一個流浪'
'。這會導致語法錯誤的方式,你有它。 – Spudley當您正確縮進代碼時,您的生活將會變得更加輕鬆。 –