steps.php未定義的變量和不確定的指數
<?php
function allowed_in($steps){
$steps = array('Text1.php', 'Text2.php', 'Text3.php');
$latestStep = $_SESSION['latestStep'];
}
?>
Text1.php:
<?php
if(allowed_in($steps)){
//all code in the create_session.php
}
?>
我試圖存儲PHP變量vairable會話和訪問功能另一頁,但我收到以下錯誤:
Notice: Undefined variable: steps in ... on line 28
Notice: Undefined index: latestStep in ... on line 49
我只需要問問如何解決這些錯誤。我需要if(isset())
爲$_SESSION
變量嗎?
UPDATE:
下面是完整的代碼:
<?php
function allowed_in($steps){
$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 1;
} else {
return 0;
}
}
?>
create_session.php:
if(allowed_in($steps)){
//all code in the create_session.php
}else{
?>
<div class="boxed">
<a href="<?= $pages[$currentPages+1] ?>">Continue</a>
<br/>
<a href="create_session.php" id="createLink">Create New</a>
</div>
<?php
}
?>
的僞代碼,我試圖按照:
function allowed_in($pased_vars){
//your code
if($foo){
return 1;
}else{
return 0;
}
}
on included pages
<?php
//include file with code
if(allowed_in($vars)){
//allowed
}else{
//not
}
第一種意思是'$ steps' i在執行'$ steps = ...'之前使用某個地方。第二個對於數組索引是一樣的。 – cmbuckley
'$ latestStep = isset($ _ SESSION ['latestStep'])? $ _SESSION ['latestStep']:null;' – Leri
[PHP:「Notice:Undefined variable」和「Notice:Undefined index」]的可能重複(http://stackoverflow.com/questions/4261133/php-notice-undefined -variable-and-notice-undefined-index) – Jocelyn