2013-01-04 41 views
1

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 
} 
+0

第一種意思是'$ steps' i在執行'$ steps = ...'之前使用某個地方。第二個對於數組索引是一樣的。 – cmbuckley

+0

'$ latestStep = isset($ _ SESSION ['latestStep'])? $ _SESSION ['latestStep']:null;' – Leri

+0

[PHP:「Notice:Undefined variable」和「Notice:Undefined index」]的可能重複(http://stackoverflow.com/questions/4261133/php-notice-undefined -variable-and-notice-undefined-index) – Jocelyn

回答

2

你有一個未定義的變量用作$steps和未定義的數組索引用作$_SESSION['latestStep']

而且這樣的:

function allowed_in($steps) { 
    $steps = array('Text1.php', 'Text2.php', 'Text3.php'); 

沒有任何意義。 您希望將一個變量$steps作爲參數傳遞給該函數,並且您立即將其值替換爲函數作用域內的值?爲什麼要這麼做?只是不要指望函數的任何參數,並在其中定義$steps

要解決該問題的索引,你可以使用isset()和處理會話變量沒有被設置的情況下:

if (!isset($_SESSION['latestStep'])) { /* what if not set? */ } 

,記得試圖訪問$_SESSION變量之前一直使用session_start()

對於未定義的變量誤差:

<?php 
    if(allowed_in($steps)){  
     //all code in the create_session.php    
    } 
?> 

意味着$steps沒有定義。在將它作爲參數傳遞給allowed_in函數之前,我沒有看到您定義$steps = ...的任何地方。那就是問題所在。

我建議你簡單地定義爲allowed_infunction allowed_in()調用上面的代碼爲:

if (allowed_in()) { 
    ... 
} 
+0

你能看看我的更新,以確保我是否應該做你說的第二部分修復未定義的變量錯誤? – user1830984

+0

@ user1830984,是的,你應該。 – Shoe

+1

謝謝你jeffrey – user1830984

0

你到底想幹什麼?檢查這些: -

1)首先你沒有返回任何東西到頁面。那麼如何檢查條件?

2)您是否將該頁面包含在另一個頁面中?

3)檢查你去哪裏定義的會話變量$ _SESSION [ 'latestStep']

4)檢查是否啓動會議在session_start()。

5)您正在steps.php中定義$ steps數組。那麼你傳遞給函數allowed_in從text1.php ..

+1

這個詞是'你',而不是'你'。請在這裏使用適當的英文而不是txt spk。 – vascowhite

+0

@vascowhite對不起。速記寫作練習。不會在未來的帖子中重複。 –

+0

如果你編輯它們。我會刪除我的投票。 – vascowhite