2013-01-04 5 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    
    } 
?> 

나는 vairable 세션에 PHP 변수를 저장하고있는 기능에 액세스하려고 다른 페이지가 표시되지만 다음 오류가 발생합니다.

Notice: Undefined variable: steps in ... on line 28
Notice: Undefined index: latestStep in ... on line 49

이 오류를 수정하는 방법을 묻기 만하면됩니다. $_SESSION 변수에 if(isset())이 필요합니까?

UPDATE : 아래는

전체 코드입니다 :

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 : 정의되지 않은 변수"및 "Notice : 정의되지 않은 인덱스"] (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가 정의되지 않은 것을 의미한다. $steps = ...을 정의하기 전에는 아무 것도 보지 않고 allowed_in 함수의 매개 변수로 전달합니다. 그것이 문제이다.

난 당신이 단순히 function allowed_in()으로 allowed_in을 정의하고 위의 코드를 호출하는 게 좋을 것 : 당신은 다음과 같이 할 수

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

정의되지 않은 변수 오류를 수정하는 데 두 번째 부분에 대해 언급 한 내용을 수행해야하는지 확인하려면 내 업데이트를 살펴 보시겠습니까? – user1830984

+0

@ user1830984, 예. – Shoe

+1

감사합니다 jeffrey – user1830984

0

당신이 뭘 하려는지 :

<?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 

    } 

    ?> 

의사 코드는 내가 따라하려고? 다음을 확인하십시오. -

1) 처음에는 아무것도 표시하지 않았습니다. 그러면 상태를 어떻게 확인할 수 있습니까?

2) 페이지를 다른 페이지에 포함 시켰습니까? 당신이 세션 변수 $ _SESSION의 정의 곳

3) 확인 [ '을 latestStep']

4) (당신이 세션으로 session_start을 시작 있는지 확인).

5) steps.php에서 $ steps 배열을 정의하고 있습니다. 그럼 당신은 text1.php에서 allowed_in 함수를 넘겨 줍니까.

+1

단어는 '너'가 아니라 '우'야. 여기 txt spk가 아닌 적절한 영어를 사용하십시오. – vascowhite

+0

@vascowhite 죄송합니다. 속기 연습. 미래의 글에서 반복되지 않을 것이다. –

+0

수정하면됩니다. 투표를하지 않겠습니다. – vascowhite