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変数をセッション変数に保存し、別のページの関数にアクセスしようとしていますが、次のエラーが発生します。

注意:未定義の変数:28行目の...のステップ
注意:未定義のインデックス:49行目の...のlatestStep

これらのエラーを修正する方法を尋ねる必要があります。変数にが必要if(isset())ですか?$_SESSION

アップデート:

以下は完全なコードです:

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

3 に答える 3

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_start()にアクセスする前に必ず使用することを忘れないでください。$_SESSION

未定義変数エラーの場合:

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

$steps定義されていないことを意味します。$steps = ...関数にパラメーターとして渡す前に定義した場所はどこにもありませんallowed_in。それが問題です。

上記のコードを単にallowed_inas として定義し、次のように呼び出すことをお勧めします。function allowed_in()

if (allowed_in()) {
    ...
}
于 2013-01-04T13:11:30.790 に答える
0

あなたは何をしようとしているのですか?これらを確認してください:-

1) まず、ページに何も返さなかった。では、状態をどのように確認できますか?

2) そのページを別のページに含めましたか?

3) セッション変数 $_SESSION['latestStep'] を定義した場所を確認します

4) セッション session_start() を開始したかどうかを確認します。

5) steps.php で $steps 配列を定義しています。次に、text1.php から関数 allowed_in に何を渡しますか..

于 2013-01-04T13:10:07.513 に答える
0

使用するisset

次のようにできます。

if(isset($_SESSION['latestStep'])){
   $latestStep = $_SESSION['latestStep'];
}
else{
   $latestStep = "";
}

http://php.net/manual/en/function.isset.php

于 2013-01-04T13:12:00.523 に答える